CanExecuteRoutedEventArgs.CanExecute Property

Definition

Gets or sets a value that indicates whether the RoutedCommand associated with this event can be executed on the command target.

C#
public bool CanExecute { get; set; }

Property Value

true if the event can be executed on the command target; otherwise, false. The default value is false.

Examples

The follow example creates a CanExecuteRoutedEventHandler that only returns true if the command target is a control. First the Source event data is cast to a Control. If it is a Control, CanExecute is set to true; otherwise, it is set to false.

C#
// CanExecuteRoutedEventHandler that only returns true if
// the source is a control.
private void CanExecuteCustomCommand(object sender, 
    CanExecuteRoutedEventArgs e)
{
    Control target = e.Source as Control;
    
    if(target != null)
    {
        e.CanExecute = true;
    }
    else
    {
        e.CanExecute = false;
    }
}

Remarks

Many command sources, such as MenuItem and Button, are disabled when CanExecute is false and enabled when the CanExecute is true.

Applies to

Produkt Versioner
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also