26

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......

RustyTheBoyRobot
  • 5,891
  • 4
  • 36
  • 55
Pritesh
  • 3,208
  • 9
  • 51
  • 70

2 Answers2

26

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.

CodeNaked
  • 40,753
  • 6
  • 122
  • 148
  • i have one delete command which delete row from datagrid, and when i move focus from datagrid to TEXTBOX it does not fire `CanExecuteChanged` as you have said...i have put two bookmark in side ADD and REMOVE block but can't getting pointer at that location... – Pritesh Jun 21 '11 at 14:11
  • @Pritesh - You would have to post the code you have. If the element that uses your command is not visible, then it wouldn't need to query your command. In general, you only need to fire that event when your CanExecute state changes. WPF generalized it to simply raise the event when they think the CanExecute could have changed (i.e. with focus changes, selection changes, etc). – CodeNaked Jun 21 '11 at 14:17
  • 1
    Your answer sparks off two clarification requests: 1/ "*This event is raised by the command*": If you are writing the command from scratch and don't explicitly raise the event yourself, does it mean it will never be raised? 2/ "*In general, this event simply reexposes the CommandManager.RequerySuggested event*": Default implementation of `add` / `remove` manage subscriptions to the event, e.g. `CanExecuteChanged += value` for `add`. If you overrides them like you did, how do clients subscribe to the event in the first place? – mins Jun 02 '19 at 07:43
8

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.

Rachel
  • 130,264
  • 66
  • 304
  • 490