I am adopting MVVM pattern in WPF and have learned the use of Command
. But in my implementation, the delegate I assigned to implement CanExecute
is alw
In your CanExecuteDelegate
you have hook to CommandManager.RequerySuggested
.
So, whenever CommandManager.RequerySuggested is raised your CanExecuteDelegate
will be called.
CommandManager.RequerySuggested event is raised whenever changes to the command source are detected by the command manager which ranges from Keyboard.KeyUpEvent, Mouse.ClickEvent etc.
Also, CommandManager has a static method - InvalidateRequerySuggested
which forces the CommandManager to raise the RequerySuggestedEvent. So, you can call that to validate your commands too manually.
If you want to take the control in hand for raising CanExecute, you can use the Delegate Command provided by PRISM. CanExecute
delegate will get called only when you explicitly call RaiseCanExecuteChanged()
method exposed by Delegate Command.
Incorporating comments to answer
Breakpoint is hitting every time on turning to VS since CommandManager RequerySuggested event gets called on lost focus of window and on activation property changed of window. That's why you notice that breakpoint is hitting every now and then when you move to VS since focus moves from WPF window to Visual Studio.