views:

306

answers:

3

When executing a custom RoutedUICommand manually from code-behind, like this:

MyCommands.MyCommand.Execute(parameter, target)

do I need to call the CanExecute method first or is this already done inside the Execute method?

+3  A: 

Do not assume that CanExecute will get called with Execute. The interface for ICommand does not imply that it calls CanExecute when Execute is called, so if it is important to you that it only execute when CanExecute is true, simply check it yourself.

Also, scanning the de-compiled code for RoutedUICommand, I don't see anywhere that checks CanExecute within Execute.

It is really more of the consumer's responsibility to determine when to call Execute/CanExecute.

Brian Genisio
+1  A: 

You should call CanExecute manually if you need, Execute will not check it!

ArsenMkrt
A: 

You shouldn't assume that CanExecute is called by the Execute method, since there is nothing to enforce that behavior. So IMO you should call CanExecute yourself

Thomas Levesque