tags:

views:

105

answers:

2

i am reading this tutorial

i don't understand what the below code is trying to do. 1st, the syntax reminds me properties, but with add/remove instead.

but what is CommandManager.RequerySuggested?

It delegates the event subscription to the CommandManager.RequerySuggested event. This ensures that the WPF commanding infrastructure asks all RelayCommand objects if they can execute whenever it asks the built-in commands

public event EventHandler CanExecuteChanged
{
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }
}

also, save command is configured with lambdas. 1st, there are 2 param variables. will they conflict? i cannot just do something like RelayCommand(this.Save(), this.CanSave) or is there no such syntax.

_saveCommand = new RelayCommand(param => this.Save(),
                                param => this.CanSave );
A: 

My thoughts

command is nothing but the function which is written in a class and you need to call that function

WPF just go ahead with Command

1- When you bind any command to your Control say ( Button ) , with the help of CanExcute method , wpf can enable / disable your ui control.

use this link to get more insigh, you will definatly understand by reading below artical,if still doubt than just leave a reply , i will try to explain in detail then

http://joshsmithonwpf.wordpress.com/2008/06/17/allowing-commandmanager-to-query-your-icommand-objects/

saurabh
just like my reply to @Goblin, what is add/remove here? a getter/setter but for add/remove event handlers? is value an EventHandler or bool (whether CanExecute is true or false). also, whether value is an EventHandler or bool, how will `RequerySuggested` know for which control is it supposed to enable disable
jiewmeng
+1  A: 
  1. CommandManager.RequerySuggested += value means that if the function for CanExecute can resolve to both true and false depending on some conditions - WPF will disable the Button/MenuItem (CommandButtonBase) if it evaluates to false and enable whenever the condition evaluates to true. If you don't have those two lines, WPF will ask the command only once (when the Button/MenuItem is loaded and will not update after that unless you do it manually.
  2. The two parameters (lambda-expressions) are of type Action and a Predicate respectively, so they cannot by definition conflict (params is just a name - and as the two functions have different scope - they don't conflict). If you have a method with the right signature, you can use that in the constructor (private void Save(object obj( and private bool CanSave(object obj) respectively) but you shouldn't have the () at the end - so new RelayCommand(this.Save,this.CanSave) should work.
Goblin
1 thing i dont understand about `CanExecuteChanged` is the add/remove. it looks like to me a getter/setter, when i add an event handler for `CanExecuteChanged` i also add it to the `CommandManager.RequerySuggested` event. same for remove? but you say value can evaluate to true/false. so what is the add/remove actually? also assuming value is bool, how will `CommandManager` know which control to disable?
jiewmeng
The add/remove is what happens everytime something adds a command.CanExecuteChanged += SomeRoutine (and -= of course). So, everytime the WPF engine adds a handler to the command (that's what happens when you add it to a button's Command property behind the scenes), we add a hook to CommandManager - this hook contains the value (ie. the button) and the Command - and then afterwards when CommandManager decides enough has changed that CanExecute could be different - it tells the Button to ask the Command for the new CanExecute - thus dis-/enabling the button accodringly.
Goblin