WPF - How to force a Command to re-evaluate 'CanExecute' via its CommandBindings

Drew Noakes picture Drew Noakes · Aug 27, 2009 · Viewed 82.1k times · Source

I have a Menu where each MenuItem in the hierarchy has its Command property set to a RoutedCommand I've defined. The associated CommandBinding provides a callback for the evaluation of CanExecute which controls the enabled state of each MenuItem.

This almost works. The menu items initially come up with the correct enabled and disabled states. However when the data that my CanExecute callback uses changes, I need the command to re-request a result from my callback in order for this new state to be reflected in the UI.

There do not appear to be any public methods on RoutedCommand or CommandBinding for this.

Note that the callback is used again when I click or type into the control (I guess it's triggered on input because mouse-over doesn't cause the refresh).

Answer

Arcturus picture Arcturus · Aug 27, 2009

Not the prettiest in the book, but you can use the CommandManager to invalidate all commandbinding:

CommandManager.InvalidateRequerySuggested();

See more info on MSDN