How do I programmatically fire a command
Asked Answered
H

4

9

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

Hersch answered 15/11, 2009 at 23:10 Comment(1)
If the Execute() method isn't doing what you expect, it might help for us to have source code to look at.Specialize
M
24

Try calling the Execute method.

Minyan answered 15/11, 2009 at 23:21 Comment(0)
I
8

Assuming there is someCommand with commandArgs:

if (someCommand.CanExecute(commandArgs))
{
    someCommand.Execute(commandArgs);
}
Ipa answered 15/11, 2009 at 23:28 Comment(0)
S
4

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).

Saddle answered 15/11, 2009 at 23:49 Comment(0)
V
0

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...

Villar answered 16/11, 2009 at 3:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.