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.
Not sure if you mean:
with c#6 and later (as proposed by eirik) there is the short form:
Update
As proposed by @Benjol, providing the button’s
CommandParameter-property value can be required in some situations and the addition of it instead ofnullmay be considered to be used as the default pattern: