ICommandinterface is the canonical way to expose commands from a ViewModel that is bound to a View. They come with a few caveats:
ICommandimplementation is not worth it.
ICommandinterface reference WPF types with the risk of complicating the required testing infrastructure.
DoSomethingmethod can be bound to a
Buttonon the View in the following manner:
AutoCommandBindingmarkup extension will dynamically build a DelegateCommand that wraps at runtime the method invocation.
ICommandinterface exposes a
CanExecute(object)method that the WPF inteface can call to detect if the command is available in the current context and thus decide if the WPF element bound to a command should be enabled or not. Using the same approach as above a ViewModel can expose a
boolproperty as following:
Can. No changes to the XAML markup are required.
Can*boolean property whenever we decide the command status changes. We can leverage the power of Radical properties metadata, and specifically the cascade changes notification feature as following:
SelectedEntityproperty changes a
INotifyPropertyChanged.PropertyChangedevent is raised also for the
CanEditproperty, thus WPF reevaluates the property and based on the boolean result the bound command will be enabled or not.