Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CanExecuteChanged event of ICommand

Tags:

Icommand contains two methods and one event.

What the two methods do is clear, but I can’t understand what the event does that is provided in ICommand.

When is the CanExecuteChanged event raised?

The below explanation is on MSDN but I can’t understand it.

CanExecuteChanged is raised if the command manager that centralizes the commanding operations detects a change in the command source that might invalidate a command that has been raised but not yet executed by the command binding.

Can you please explain this in simple terms?

Thanks......

like image 385
Pritesh Avatar asked Jun 21 '11 13:06

Pritesh


People also ask

How do you use Canexecutechanged?

Passing a parameter to the CanExecute and Execute methodsA parameter can be passed through the "CommandParameter" property. Once the button is clicked the selected address value is passed to the ICommand. Execute method. The CommandParameter is sent to both CanExecute and Execute events.

What is ICommand C#?

The ICommand interface is the code contract for commands that are written in . NET for Windows Runtime apps. These commands provide the commanding behavior for UI elements such as a Windows Runtime XAML Button and in particular an AppBarButton .

Can execute changed WPF?

No you can not use it to change the can execute state. It is an event and objects which participate in the ICommand pattern can choose to listen to this event e.g. a button may use this event to know when to re-query the commands state (by calling the can execute method) to set its enabled state.

What is MVVM command?

Commands are an implementation of the ICommand interface that is part of the . NET Framework. This interface is used a lot in MVVM applications, but it is useful not only in XAML-based apps.


2 Answers

This event is raised by the command to notify it's consumers (i.e. Button, MenuItem) that it's CanExecute property may have changed. So if focus is moved from one TextBox to another, your command may need to be enabled/disabled. This information also needs to be passed to any controls using your command.

In general, this event simply reexposes the CommandManager.RequerySuggested event. From the RoutedCommand class:

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

The RequerySuggested event is fired quite often, as focus is moved, text selection is changed. This can also be manually raised by calling InvalidateRequerySuggested.

like image 73
CodeNaked Avatar answered Sep 28 '22 18:09

CodeNaked


CanExecuteChanged is raised when the CanExecute method of an ICommand gets changed

In some 3rd party libraries, the CanExecuteChanged event also gets raised if the CanExecute parameters raise a PropertyChanged event. For example, MVVM Light Toolkit's RelayCommand raises the CanExecuteChanged event if the CanExecute parameters raise a PropertyChanged event, while Prism's DelegateCommand does not.

like image 21
Rachel Avatar answered Sep 28 '22 17:09

Rachel