Specify Command for MenuItem in a DataTemplate

后端 未结 3 471
花落未央
花落未央 2020-12-09 16:49

I have a context menu. It\'s bound to some collection and it has a defined ItemTemplate like this:



        
3条回答
  •  我在风中等你
    2020-12-09 17:34

    Although this is only a slight variation on Martin Harris's answer, I thought I'd share it anyway. I found it more useful specify a single command for the whole collection and also send along a CommandParameter:

    
        
    
    

    Then you can determine what to do in the handler for the command:

    private void CommandForAll_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        var cmdParam = e.Paramater as ExpectedType
        if (cmdParam != null)
            //DoStuff...
    }
    

提交回复
热议问题