How to implement commands to use ancestor methods in WPF?

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

I have this context menu resource:



        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-20 18:42

    Unfortunately you cannot bind Executed for a ContextMenu as it is an event. An additional problem is that the ContextMenu does not exist in the VisualTree the rest of your application exists. There are solutions for both of this problems.

    First of all you can use the Tag property of the parent control of the ContextMenu to pass-through the DataContext of your application. Then you can use an DelegateCommand for your CommandBinding and there you go. Here's a small sample showing View, ViewModel and the DelegateCommand implementation you would have to add to you project.

    DelegateCommand.cs

    public class DelegateCommand : ICommand
    {
        private readonly Action execute;
        private readonly Predicate canExecute;
    
        public DelegateCommand(Action execute)
            : this(execute, null)
        { }
    
        public DelegateCommand(Action execute, Predicate canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
    
            this.execute = execute;
            this.canExecute = canExecute;
        }
    
        #region ICommand Members
    
        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return canExecute == null ? true : canExecute(parameter);
        }
    
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
        public void Execute(object parameter)
        {
            execute(parameter);
        }
    
        #endregion
    }
    
    
    

    MainWindowView.xaml

    
        
            
                
                    
                
            
        
        
            
                
                    
                        
                    
                
            
        
    
    

    MainWindowView.xaml.cs

    public partial class MainWindowView : Window
    {
        public MainWindowView()
        {
            InitializeComponent();
            DataContext = new MainWindowViewModel();
        }
    }
    

    MainWindowViewModel.cs

    public class MainWindowViewModel
    {
        public ObservableCollection FooViewModels { get; set; }
    
        public MainWindowViewModel()
        {
            FooViewModels = new ObservableCollection();
        }
    }
    

    FooViewModel.cs

    public class FooViewModel
    {
        public ICommand HelpExecuted { get; set; }
    
        public FooViewModel()
        {
            HelpExecuted = new DelegateCommand(ShowHelp);
        }
    
        private void ShowHelp(object obj)
        {
            // Yay!
        }
    }
    

    提交回复
    热议问题