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 です。

次の例では、コマンド ターゲットがコントロールの場合にのみ true を返す CanExecuteRoutedEventHandler を作成します。 まず、 Source イベント データが Controlにキャストされます。 Controlの場合、CanExecutetrue に設定されます。それ以外の場合は、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などの多くのコマンド ソースは、CanExecutefalseされると無効になり、CanExecutetrueされると有効になります。

適用対象

こちらもご覧ください