开发者

(WPF)how to raise Datatemplate's Context MenuItem event

开发者 https://www.devze.com 2022-12-21 04:18 出处:网络
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.

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
            {
            }
        }
开发者_如何学JAVA

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.

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

Thanks.


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>

Here is the code-behind:

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.
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消