This documentation is archived and is not being maintained.

RoutedCommand.CanExecuteChanged Event

Occurs when changes occur which affect whether or not the command should execute on the current command target has changed.

Namespace: System.Windows.Input
Assembly: PresentationCore (in presentationcore.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public event EventHandler CanExecuteChanged
/** @event */
public final void add_CanExecuteChanged (EventHandler value)

/** @event */
public final void remove_CanExecuteChanged (EventHandler value)

In JScript, you can handle the events defined by a class, but you cannot define your own.
<object CanExecuteChanged="EventHandler" .../>

The RoutedCommand listens for the RequerySuggested event which is raised by the CommandManager when conditions are met that may change the whether or not the command should execute, such as a change in keyboard focus. When the command receives this event, it raises the CanExecuteChanged event. Normally, the command source will listen for this event and query the RoutedCommand by way of the CanExecute method. Most command sources will disable themselves if the command cannot be executed. An example of this is when a MenuItem grays itself out when the command cannot be executed.

In some situations, the CommandManager is unaware of a change in conditions that alters the ability of a command to execute. In these cases, you can force the CommandManager to raises the RequerySuggested event by calling the InvalidateRequerySuggested method, this will in turn cause the RoutedCommand to raise the CanExecuteChanged event.

The following example is a CanExecuteChanged event handler from a custom implementation of ICommandSource. The full sample subclasses the Slider class and implements ICommandSource to create a slider that invokes a command when its value property is changed. For the complete sample, see Implement ICommand Source Sample.

this.Command in this example is the Command property on the ICommandSource. If the command is not a null reference (Nothing in Visual Basic), the command is cast to a RoutedCommand. If the command is a RoutedCommand, then the CanExecute method is called passing the CommandTarget and the CommandParameter. If command is not a RoutedCommand, it is cast to an ICommand and the CanExecute method is called passing the CommandParameter.

If the can execute method returns true, then the control is enabled; otherwise, the control is disable.

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;
            }
        }
    }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Show: