CanExecuteRoutedEventArgs.CanExecute 属性

定义

获取或设置一个值,该值指示是否可以 RoutedCommand 在命令目标上执行与此事件关联的事件。

public:
 property bool CanExecute { bool get(); void set(bool value); };
public bool CanExecute { get; set; }
member this.CanExecute : bool with get, set
Public Property CanExecute As Boolean

属性值

true 如果可以在命令目标上执行事件,则为否则,为 false. 默认值为 false

示例

以下示例创建仅在 CanExecuteRoutedEventHandler 命令目标为控件时返回 true 的值。 首先,事件Source数据被强制转换为 .Control 如果它是一个 ControlCanExecute 则设置为 true;否则,它设置为 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;
    }
}
' CanExecuteRoutedEventHandler that only returns true if
' the source is a control.
Private Sub CanExecuteCustomCommand(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
    Dim target As Control = TryCast(e.Source, Control)

    If target IsNot Nothing Then
        e.CanExecute = True
    Else
        e.CanExecute = False
    End If
End Sub

注解

许多命令源(例如MenuItem,以及在Button何时启用)处于禁用CanExecutefalse状态CanExecutetrue

适用于

另请参阅