With version 4.0 certain Silverlight controls have a Command property that is of type ICommand. There is, however, still a huge gap between the Silverlight and WPF commanding infrastructure. DotNetBar for Silverlight attempts to close that gap at least a little with the CommandManager and the RelayCommand.

DevComponents.Silverlight.Controls.CommandManager is essentially a scaled down version of the WPF class of the same name. It exposes one static method – InvalidateRequerySuggested and one static event – RequerySuggested.

CommandManager.RequerySuggested is raised as a signal that UI events have taken place which may have altered a command’s (any command) ability to execute – and that it would be a good idea to re-query the command’s CanExecute property to determine if this is in fact the case. A command’s CanExecute property value can be a basis, for example, for enabling or disabling a Button. The first time an event handler is added to the delegate list of RequerySuggested the class begins listening to a set of UI events which typically go along with the kind of state change that affect a Command’s ability to execute. (The specific events are GotFocus, LostFocus, MouseLeftButtonUpEvent, KeyDownEvent – all on the RootVisual.)

DevComponents.Silverlight.Controls.RelayCommand is an implementation of interface ICommand based on the command of the same name originally described here. With the RelayCommand, you provide a delegate to be executed when the command’s execute method is called, and a separate, optional, delegate to be called when the command’s CanExecute method is called. Because execution logic is not a part of the command’s definition, RelayCommand is very flexible.

RelayCommand is hooked into Command Manager’s RequerySuggeted event by way of its own event CanExecuteChanged. Here is the code which does it:

/// <summary>
/// Event raised when the value of CanExecute MIGHT have changed.
/// </summary>
public event EventHandler CanExecuteChanged
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }

Related posts:

  1. Routed Events
  2. Typed Data Templates
  3. ItemsHostControl Control