in my WPF UI, I use RoutedCommands that I refer to in my xaml via the following code:
Command="viewModel:MessageListViewModel.DeleteMessagesCommand"
I don't like this static link to my ViewModel class,I think this is not as nice as creating a custom ICommand implementation and use a syntax like the following
Command="{Binding DeleteMessagesCommand}"
Having created one, I notice one major drawback of what I've done: RoutedCommands utilize the CommandManager and (in some way that is completely opaque to me) fire the CommandManager.RequerySuggested event, so that their CanExecute Method is requeried automatically. As for my custom implementation, CanExecute is only fired once at startup and never again after that.
Does anybody have an elegant solution for this?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With