Export (0) Print
Expand All

CanExecuteRoutedEventArgs Class

Provides data for the CanExecute and PreviewCanExecute routed events.

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

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

public sealed class CanExecuteRoutedEventArgs : RoutedEventArgs

The CanExecuteRoutedEventArgs type exposes the following members.

  NameDescription
Public propertyCanExecuteGets or sets a value that indicates whether the RoutedCommand associated with this event can be executed on the command target.
Public propertyCommandGets the command associated with this event.
Public propertyContinueRoutingDetermines whether the input routed event that invoked the command should continue to route through the element tree.
Public propertyHandledGets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. (Inherited from RoutedEventArgs.)
Public propertyOriginalSourceGets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. (Inherited from RoutedEventArgs.)
Public propertyParameterGets the command specific data.
Public propertyRoutedEventGets or sets the RoutedEvent associated with this RoutedEventArgs instance. (Inherited from RoutedEventArgs.)
Public propertySourceGets or sets a reference to the object that raised the event. (Inherited from RoutedEventArgs.)
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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

.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.

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