CommandManager Executed Events don't fire for custom ICommands

Posted by Andre Luus on Stack Overflow See other posts from Stack Overflow or by Andre Luus
Published on 2010-06-15T08:09:49Z Indexed on 2010/06/15 8:12 UTC
Read the original article Hit count: 333

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf