Export (0) Print
Expand All

ICommandSource Interface

Defines an object that knows how to invoke a command.

Namespace:  System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)

public interface ICommandSource

The ICommandSource type exposes the following members.

  NameDescription
Public propertyCommandGets the command that will be executed when the command source is invoked.
Public propertyCommandParameterRepresents a user defined data value that can be passed to the command when it is executed.
Public propertyCommandTargetThe object that the command is being executed on.
Top

The command source defines how a command is invoked by that particular object. For example, if a Button is associated with a command, the command is invoked when the Button is clicked.

A command source will normally disable itself if the command it is associated with cannot execute on the current command target. For example, a MenuItem associated with the Paste command will gray itself out when the Paste command cannot execute on the current command target.

Normally, a command source will listen to the CanExecuteChanged event on the command. This informs the command source when conditions change on the command target, such as loss of keyboard focus. The command source can then query the command using the CanExecute method.

Some of the classes in WPF that implement ICommandSource are: ButtonBase, MenuItem, and Hyperlink.

In the Windows Presentation Foundation (WPF) commanding system, the CommandTarget property on a ICommandSource is only applicable when the ICommand is a RoutedCommand. If the CommandTarget is set on a ICommandSource and the corresponding command is not a RoutedCommand, the command target is ignored.

This example shows how to create a command source by implementing ICommandSource. A command source is an object that knows how to invoke a command. The ICommandSource interface exposes three members: Command, CommandParameter, and CommandTarget. Command is the command which will be invoked. The CommandParameter is a user-defined data type which is passed from the command source to the method which handles the command. The CommandTarget is the object that the command is being executed on.

In this example, a class is created which subclasses the Slider control and implements ICommandSource.

WPF provides a number of classes which implement ICommandSource, such as ButtonMenuItem, and ListBoxItem. A command source defines how it invokes a command. Button and MenuItem invoke a command when they are clicked. A ListBoxItem invokes a command when it is double clicked. These classes only become a command source when their Command property is set.

For this example we will invoke the command when the slider is moved, or more accurately, when the Value property is changed.

The following is the class definition.

public class CommandSlider : Slider, ICommandSource
{
    public CommandSlider() : base()
    {

    }

The next step is to implement the ICommandSource members. In this example, the properties are implemented as DependencyProperty objects. This enables the properties to use data binding. For more information about the DependencyProperty class, see the Dependency Properties Overview. For more information about data binding, see the Data Binding Overview.

Only the Command property is shown here.

// Make Command a dependency property so it can use databinding. 
public static readonly DependencyProperty CommandProperty =
    DependencyProperty.Register(
        "Command",
        typeof(ICommand),
        typeof(CommandSlider),
        new PropertyMetadata((ICommand)null,
        new PropertyChangedCallback(CommandChanged)));

public ICommand Command
{
    get 
    {
        return (ICommand)GetValue(CommandProperty);
    }
    set 
    {
        SetValue(CommandProperty, value);
    }
}

The following is the DependencyProperty change callback.

// Command dependency property change callback. 
private static void CommandChanged(DependencyObject d,
    DependencyPropertyChangedEventArgs e)
{
    CommandSlider cs = (CommandSlider)d;
    cs.HookUpCommand((ICommand)e.OldValue,(ICommand)e.NewValue);
}

The next step is to add and remove the command which is associated with the command source. The Command property cannot simply be overwritten when a new command is added, because the event handlers associated with the previous command, if there was one, must be removed first.

// Add a new command to the Command Property. 
private void HookUpCommand(ICommand oldCommand, ICommand newCommand)
{
    // If oldCommand is not null, then we need to remove the handlers. 
    if (oldCommand != null)
    {
        RemoveCommand(oldCommand, newCommand);
    }
    AddCommand(oldCommand, newCommand);
}

// Remove an old command from the Command Property. 
private void RemoveCommand(ICommand oldCommand, ICommand newCommand)
{
    EventHandler handler = CanExecuteChanged;
    oldCommand.CanExecuteChanged -= handler;
}

// Add the command. 
private void AddCommand(ICommand oldCommand, ICommand newCommand)
{
    EventHandler handler = new EventHandler(CanExecuteChanged);
    canExecuteChangedHandler = handler;
    if (newCommand != null)
    {
        newCommand.CanExecuteChanged += canExecuteChangedHandler;
    }
}

The last step is to create logic for the CanExecuteChanged handler and the Execute method.

The CanExecuteChanged event notifies the command source that the ability of the command to execute on the current command target may have changed. When a command source receives this event, it typically calls the CanExecute method on the command. If the command cannot execute on the current command target, the command source will typically disable itself. If the command can execute on the current command target, the command source will typically enable itself.

private void CanExecuteChanged(object sender, EventArgs e)
{

    if (this.Command != null)
    {
        RoutedCommand command = this.Command as RoutedCommand;

        // If a RoutedCommand. 
        if (command != null)
        {
            if (command.CanExecute(CommandParameter, CommandTarget))
            {
                this.IsEnabled = true;
            }
            else
            {
                this.IsEnabled = false;
            }
        }
        // If a not RoutedCommand. 
        else
        {
            if (Command.CanExecute(CommandParameter))
            {
                this.IsEnabled = true;
            }
            else
            {
                this.IsEnabled = false;
            }
        }
    }
}

The last step is the Execute method. If the command is a RoutedCommand, the RoutedCommand Execute method is called; otherwise, the ICommand Execute method is called.

// If Command is defined, moving the slider will invoke the command; 
// Otherwise, the slider will behave normally. 
protected override void OnValueChanged(double oldValue, double newValue)
{
    base.OnValueChanged(oldValue, newValue);

    if (this.Command != null)
    {
        RoutedCommand command = Command as RoutedCommand;

        if (command != null)
        {
            command.Execute(CommandParameter, CommandTarget);
        }
        else
        {
            ((ICommand)Command).Execute(CommandParameter);
        }
    }
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft