Language

RoutedCommand.CanExecute(Object, IInputElement) メソッド

定義

この RoutedCommand を現在の状態で実行できるかどうかを判断します。

public:
 bool CanExecute(System::Object ^ parameter, System::Windows::IInputElement ^ target);
[System.Security.SecurityCritical]
public bool CanExecute(object parameter, System.Windows.IInputElement target);
public bool CanExecute(object parameter, System.Windows.IInputElement target);
[<System.Security.SecurityCritical>]
member this.CanExecute : obj * System.Windows.IInputElement -> bool
member this.CanExecute : obj * System.Windows.IInputElement -> bool
Public Function CanExecute (parameter As Object, target As IInputElement) As Boolean

パラメーター

parameter
Object

ユーザー定義のデータ型。

target
IInputElement

コマンド ターゲット。

返品

true コマンドが現在のコマンド ターゲットで実行できる場合。それ以外の場合は false。

属性

例外

target が UIElement または ContentElementではありません。

例

次の例は、CanExecuteChangedのカスタム実装からのICommandSource イベント ハンドラーです。

this.Commandこの例では、Commandの ICommandSource プロパティです。 コマンドが nullされていない場合、コマンドは RoutedCommandにキャストされます。 コマンドがRoutedCommandの場合は、CanExecuteとCommandTargetを渡すCommandParameter メソッドが呼び出されます。 コマンドがRoutedCommandでない場合は、ICommandにキャストされ、CanExecuteを渡すCommandParameter メソッドが呼び出されます。

CanExecute メソッドがtrueを返した場合、コントロールは有効になります。それ以外の場合、コントロールは無効になります。

private void CanExecuteChanged(object sender, EventArgs e)
{

    if (this.Command != null)
    {
        RoutedCommand command = this.Command as RoutedCommand;

        // If a RoutedCommand.
        if (command != null)
        {
            if (command.CanExecute(CommandParameter, CommandTarget))
            {
                this.IsEnabled = true;
            }
            else
            {
                this.IsEnabled = false;
            }
        }
        // If a not RoutedCommand.
        else
        {
            if (Command.CanExecute(CommandParameter))
            {
                this.IsEnabled = true;
            }
            else
            {
                this.IsEnabled = false;
            }
        }
    }
}
Private Sub CanExecuteChanged(ByVal sender As Object, ByVal e As EventArgs)

    If Me.Command IsNot Nothing Then
        Dim command As RoutedCommand = TryCast(Me.Command, RoutedCommand)

        ' If a RoutedCommand.
        If command IsNot Nothing Then
            If command.CanExecute(CommandParameter, CommandTarget) Then
                Me.IsEnabled = True
            Else
                Me.IsEnabled = False
            End If
            ' If a not RoutedCommand.
        Else
            If Me.Command.CanExecute(CommandParameter) Then
                Me.IsEnabled = True
            Else
                Me.IsEnabled = False
            End If
        End If
    End If
End Sub

注釈

現在のコマンド ターゲットでRoutedCommandを実行できるかどうかを決定する実際のロジックは、CanExecute メソッドに含まれず、CanExecutePreviewCanExecuteと、CanExecuteを持つオブジェクトを探す要素ツリーを通じてトンネルとバブルを行うCommandBindingイベントを発生させます。 そのCommandBindingのRoutedCommandが見つかった場合は、CanExecuteRoutedEventHandlerにアタッチされているCommandBindingが呼び出されます。 これらのハンドラーは、 RoutedCommand が実行できるかどうかを判断するためのプログラミング ロジックを提供します。

PreviewCanExecuteイベントとPreviewExecuted イベントは、CommandTargetで発生します。 CommandTargetでICommandSourceが設定されていない場合、キーボード フォーカスのある要素でPreviewCanExecuteイベントとCanExecute イベントが発生します。

適用対象