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 事件資料會轉換成 ControlControl如果是 ,CanExecute則會設定為 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

備註

當 為 false 時,會停用許多命令來源,例如 MenuItemButton,並在 為時trueCanExecuteCanExecute啟用。

適用於

另請參閱