The WPF CommandManager allows you to do the following (pseudo-ish-code):
<Button Name="SomeButton" Command="{Binding Path=ViewModelCommand}"/>
And in the code-behind:
private void InitCommandEvents()
{
CommandManager.AddExecutedEventHandler(this.SomeButton, SomeEventHandler);
}
The SomeEventHandler
never gets called.
To me this didn't seem like something very wrong to try and do, but if you consider what happens inside CommandManager.AddExecutedEventHandler
, it makes sense why it doesn't. Add to that the fact that the documentation clearly states that the CommandManager only works with RoutedCommands.
Nonetheless, this had me very frustrated for a while and led me to this question:
What would you suggest is the best workaround for the fact that the CommandManager does not support custom ICommands? Especially if you want to add behavior around the execution of a command?
For now, I fire the command manually in code behind from the button click event.