views:

192

answers:

1

Hi, I tried with a minimal application, just a textbox and a button. The button is binded to a RelayCommand instance, and the CanExecute method just return true or false with a random. The textbox text is binded with a string property.

What's making me mad is that the CanExecute method is "always" called: a change of the element focused, a key pressed in the textbox, it seems that everything fires a call to my CanExecute method. Is this a "feature" of the mvvm light toolkit? Does this happen in a "normal" wpf application?

Yes, I know, I think I should know more about the commandind system in wpf... ;-)

Thanks for answers!

David

A: 

CanExecute is used to determine if the current state allows the command to execute. This is usually tied to the IsEnabled property to disable the command.

This should also be tied to a property on your ViewModel that indicates if the current view state allows executing.

It is tied to any event trigger on the hosting window, since any event could cause a change to the CanExecute state.

This link confirms it is fired on any event in the hosting window.

http://robburke.net/2008/04/23/wpf-command-pattern-when-does-it-query-canexecute/

Why do you think it being called all the time is an issue? As long as you don't have any intense logic in the bound property it should be fine.

Kenoyer130
Thanks for immediate answer. It's not an issue, at least until I'll have tons of commands. But now I'm asking why MS put a CanExecuteChanged event in the ICommand interface... ;-)
David