MVVM - WPF Commands and RelayCommand

Oct 29, 2010 at 8:42 PM

I have been reading Josh Smith's article in Feb 2009 MSDN magazine about Model-View-ViewModel, and running the code. He embeds a few fields on a tabControl page to illustrate the principles. There are a couple things I don't understand:

1) What are the triggers that execute SaveCommand? In general the Microsoft documentation says a Command triggers each time the control is clicked. Of-course there is no click here. I can see the SaveCommand executes each time a new Tab page is created (i.e. a new customer tab is created). It also triggers each time I switch from one tab to another. But it is not triggered when I minimize the window.

2) Follow-up to above question: when CanSave becomes true and I actually Click on the button, SaveCommand is not triggerred! Of-course the this.Save() is executed, as expected, but since Microsoft says a click triggers the command I am confused. Perhaps the explanation is as simple as the click triggers the declared method, this.Save(), not the ICommand itself?

3) Each keystroke in any of the input fields on the tab cause this.CanSave() to execute. I am a newcomer to Commands but how does this single reference to CanSave() within the SaveCommand code trigger CanSave() on every keystroke?



<Button Command="{Binding Path=SaveCommand}" Content="_Save" />

 public ICommand SaveCommand
                if (_saveCommand == null)
                     _saveCommand = new RelayCommand(
                          param => this.Save(),
                          param => this.CanSave
                return _saveCommand;