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

注解

许多命令源(如 MenuItemButton)在 时falseCanExecute被禁用,在 为 trueCanExecute启用。

适用于

另请参阅