tags:

views:

54

answers:

2

I have a button:

<Button x:Name="MyButton" Command="SomeCommand"/>

Is there a way to execute the command from source? Calling the click on the button does not help:

MyButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));

I mean - this does raise the event, but it does not raise the command. Is there something similar to this RaiseEvent but just for Command? If there is not - how can I instantiate ExecutedRoutedEventArgs? Is it possible?

Lastly - please do not tell me how to avoid calling the command.

+3  A: 

You need ICommand.Execute(object) to accomplish that.

Working example for your sample code: this.MyButton.Command.Execute(null);

BlueCode
A: 

Not sure if you mean:

if(null != MyButton.Command){
    MyButton.Command.Execute(null);
}
HCL
Thanks. That is so easy yet I did not manage to think of that!
Jefim