Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

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

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
            {
            }
        }

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...