CanExecute Property
Collapse the table of content
Expand the table of content
This documentation is archived and is not being maintained.

CanExecuteRoutedEventArgs.CanExecute Property

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

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

public bool CanExecute { get; set; }
/** @property */
public boolean get_CanExecute ()

/** @property */
public void set_CanExecute (boolean value)

public function get CanExecute () : boolean

public function set CanExecute (value : boolean)

Members of this class are either not typically used in XAML, or cannot be used in XAML.

Property Value

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

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

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.

// CanExecuteRoutedEventHandler that only returns true if
// the source is a control.
public void CanExecuteCustomCommand(object sender, 
    CanExecuteRoutedEventArgs e)
{
    Control target = e.Source as Control;
    
    if(target != null)
    {
        e.CanExecute = true;
    }
    else
    {
        e.CanExecute = 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:
© 2016 Microsoft