How to implement commands to use ancestor methods in WPF?

后端 未结 5 1939
萌比男神i
萌比男神i 2020-12-20 18:11

I have this context menu resource:



        
5条回答
  •  梦毁少年i
    2020-12-20 18:58

    You are getting this error because CommandBinding.Executed is not dependency property so you cannot bind to it.

    Instead, use ResourceDictionary code behind to specify event handler for CommandBinding.Executed event, and in the event handler code call FooViewModel.HelpExecuted() method like this:

    MainWindowResourceDictionary.xaml

    
    
        
            
                
            
        
    
        
            
                
            
            
        
    
    
    

    MainWindowResourceDictionary.xaml.cs

    public partial class MainWindowResourceDictionary : ResourceDictionary
    {
        public MainWindowResourceDictionary()
        {
            InitializeComponent();
        }
    
        private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            var fooViewModel = (FooViewModel)((FrameworkElement)e.Source).DataContext;
            fooViewModel.HelpExecuted();
        }
    }
    

提交回复
热议问题