(WPF)如何引发Datatemplate的Context MenuItem事件 [英] (WPF)how to raise Datatemplate's Context MenuItem event

查看:43
本文介绍了(WPF)如何引发Datatemplate的Context MenuItem事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框及其数据模板.列表框的项目与静态源绑定.在数据模板中,我有一个按钮,并创建了它的上下文菜单和一些菜单项.

I have a listbox and its data template. Items of listbox are binded with a static source. In data template I have a button, and created context menu of it and some menu items.

<ObjectDataProvider x:Key="GlobalUnits"/>
<DataTemplate x:Key="listboxTemplate" DataType="UnitClass">
      <StackPanel Margin="3" >
        <DockPanel >
          <Button DockPanel.Dock="Left" Margin="5,0,10,0">
            <Button.ContextMenu>
              <ContextMenu x:Name="CMUnits"  Opened="OnContextMenuOpened"
                     MenuItem.Click="OnMenuItemClick">
                <MenuItem Header="Add"
                      x:Name="MenuItemName" />
                <MenuItem Header="Delete"
                      x:Name="MenuItemDelete" />                
              </ContextMenu>
            </Button.ContextMenu>
          </Button>          
        </DockPanel>        
      </StackPanel>
    </DataTemplate>

<ListBox x:Name="TUListBox"
         local:DragDropManager.DragSourceAdvisor="{StaticResource sourceAdvisor}"
         ItemTemplate="{DynamicResource listboxTemplate}"
         ItemsSource="{Binding Source={StaticResource GlobalUnits}}"/>

In Code behind:

void OnMenuItemClick(object sender, RoutedEventArgs e)
        {
           if (e.Source == this.MenuItemDelete) <//error  here MenuItemDelete is not available
            {
            }
        }

我已经在datatemplate中定义了MenuItemDelete作为按钮作为menuitem.有任何建议,如果我将上下文菜单放在普通xaml代码中的datatemplate之外的话,它将起作用.

I have defined MenuItemDelete in datatemplate for a button as menuitem. Any suggestion, if I put context menu out of datatemplate in normal xaml code it works.

当我在运行时单击菜单项时,我的程序调试光标甚至没有到达OnMenuItemClick事件

Well my program debug cursor does not even reach to OnMenuItemClick event when i click on menu item in runtime

谢谢.

推荐答案

对于您的问题,有更好的解决方案,涉及使用命令(您应该研究一下),但这是我创建的一个示例,用于演示此工作.这是XAML:

There are better solutions to your problem that involve using commands (which you should look into) but here is an example I have created that demonstrates this working. Here is the XAML:

<Window x:Class="TestWpfApplication.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window15" Height="300" Width="300"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Window.Resources>
        <DataTemplate x:Key="testTemplate">
            <StackPanel Margin="3">
                <DockPanel>
                    <Button DockPanel.Dock="Left" Margin="5,0,10,0" Content="{Binding}">
                        <Button.ContextMenu>
                            <ContextMenu x:Name="CMUnits" MenuItem.Click="OnMenuItemClick">
                                <MenuItem Header="Add"
                                          x:Name="MenuItemAdd" />
                                <MenuItem Header="Delete"
                                          x:Name="MenuItemDelete" />
                            </ContextMenu>
                        </Button.ContextMenu>
                    </Button>
                </DockPanel>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ListBox ItemTemplate="{StaticResource testTemplate}"
                 ItemsSource="{Binding TestItems}"/>
    </Grid>
</Window>

这是背后的代码:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    public ObservableCollection<string> TestItems
    {
        get
        {
            return new ObservableCollection<string>()
            {
                "Item 1", "Item 2", "Item 3"
            };
        }
    }

    private void OnMenuItemClick(object sender, RoutedEventArgs e)
    {
        MenuItem item = e.Source as MenuItem;
        if (item.Name == "MenuItemDelete")
        {
            // Delete the item.
        }
        else if (item.Name == "MenuItemAdd")
        {
            // Add the item.
        }
    }
}

这篇关于(WPF)如何引发Datatemplate的Context MenuItem事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆