CanExecuteRoutedEventArgs Class

Provides data for the CanExecute and PreviewCanExecute routed events.

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

public sealed class CanExecuteRoutedEventArgs : RoutedEventArgs
You cannot directly create an instance of this class in XAML.

Typically, a command source, such a MenuItem, will call the CanExecute method on a RoutedCommand to determine if the command can or cannot execute on the current command target. If CanExecute is set to false from an event handler, the command source will disable itself. For example, if a MenuItem is acting as the command source for a command and the command cannot execute on the current command target, then the MenuItem will gray itself out.

The CanExecuteChanged event notifies a command source when the ability of a command to execute may have changed.

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

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

System.Object
  System.EventArgs
    System.Windows.RoutedEventArgs
      System.Windows.Input.CanExecuteRoutedEventArgs

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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