RoutedCommand.CanExecuteChanged Event

Occurs when changes to the command source are detected by the command manager. These changes often affect whether the command should execute on the current command target.

Namespace:  System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

public event EventHandler CanExecuteChanged
<object CanExecuteChanged="EventHandler" .../>

Implements

ICommand.CanExecuteChanged

The RoutedCommand listens for the RequerySuggested event, which is raised by the CommandManager. The RequerySuggested event is raised whenever conditions are met that may change the whether the command should execute, such as a change in keyboard focus. When the command receives the RequerySuggested event, it raises the CanExecuteChanged event. Generally, 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, as part of their command binding. 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 ICommandSource Sample.

this.Command in this example is the Command property on the ICommandSource. If the command is not null, 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 CanExecute method returns true, then the control is enabled; otherwise, the control is disabled.

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 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5, 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft