Language

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

定義

現在のコマンド ターゲットで RoutedCommand を実行します。

public:
 void Execute(System::Object ^ parameter, System::Windows::IInputElement ^ target);
[System.Security.SecurityCritical]
public void Execute(object parameter, System.Windows.IInputElement target);
public void Execute(object parameter, System.Windows.IInputElement target);
[<System.Security.SecurityCritical>]
member this.Execute : obj * System.Windows.IInputElement -> unit
member this.Execute : obj * System.Windows.IInputElement -> unit
Public Sub Execute (parameter As Object, target As IInputElement)

パラメーター

parameter
Object

ハンドラーに渡されるユーザー定義パラメーター。

target
IInputElement

コマンド ハンドラーの検索を開始する要素。

属性

例外

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

例

次の例は、 ICommandSource サンプルのカスタム実装の例です。

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

// If Command is defined, moving the slider will invoke the command;
// Otherwise, the slider will behave normally.
protected override void OnValueChanged(double oldValue, double newValue)
{
    base.OnValueChanged(oldValue, newValue);

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

        if (command != null)
        {
            command.Execute(CommandParameter, CommandTarget);
        }
        else
        {
            ((ICommand)Command).Execute(CommandParameter);
        }
    }
}
' If Command is defined, moving the slider will invoke the command;
' Otherwise, the slider will behave normally.
Protected Overrides Sub OnValueChanged(ByVal oldValue As Double, ByVal newValue As Double)
    MyBase.OnValueChanged(oldValue, newValue)

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

        If command IsNot Nothing Then
            command.Execute(CommandParameter, CommandTarget)
        Else
            CType(Me.Command, ICommand).Execute(CommandParameter)
        End If
    End If
End Sub

注釈

RoutedCommandを実行する実際のロジックは、Execute メソッドに含まれていません。 Execute では、 PreviewExecuted イベントと Executed イベントが発生し、要素ツリーを通ってトンネルとバブルが発生し、 CommandBindingを持つオブジェクトが検索されます。 そのCommandBindingのRoutedCommandが見つかった場合は、ExecutedRoutedEventHandlerにアタッチされているCommandBindingが呼び出されます。 これらのハンドラーは、 RoutedCommandを実行するプログラミング ロジックを提供します。

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

適用対象