WPF Custom ICommand implementation and the CanExecuteChanged event

后端 未结 2 430
耶瑟儿~
耶瑟儿~ 2021-01-05 19:14

in my WPF UI, I use RoutedCommands that I refer to in my xaml via the following code:

Command=\"viewModel:MessageListViewModel.DeleteMessagesCommand\"
         


        
2条回答
  •  半阙折子戏
    2021-01-05 19:45

    Just implement the CanExecuteChanged event as follows:

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
    

    When you assign the command to a control, it subscribes to the CanExecuteChanged event. If you "redirect" it to the CommandManager.RequerySuggested event, the control will be notified whenever CommandManager.RequerySuggested is triggered.

提交回复
热议问题