views:

285

answers:

4

i have an ICommand that I want to fire (make the execute go) from code; how do I do this?

+4  A: 

Try calling the Execute method.

SLaks
wow dont i feel a bit silly :)
Aran Mulholland
Entirely too easy! This can't be right! ;-)
senfo
+1  A: 

Assuming there is someCommand with commandArgs:

if (someCommand.CanExecute(commandArgs))
{
    someCommand.Execute(commandArgs);
}
Cameron MacFarland
+2  A: 

If you're using RoutedUICommand's Execute and CanExecute, be sure to pass in a valid target so that the correct CommandBinding can be found.

Also, if your command's handlers do not modify View objects directly, consider using Kent Boogaart's DelegateCommand. Using delegate commands will move the business logic to the ViewModel, which is nice, and they're especially handy if you need execute commands directly from code and you don't have access to the View (or a View object from which you can bubble to your CommandBindings).

IV
A: 

Remember, if you need call another event of internal components, like a click button, see the methods startin with Perform (like PerformClick of a button).

Another answers are okay, use Execute only...

MCunha98