次の方法で共有


方法: コマンドをサポートしていないコントロールにコマンドをフックする

次の例は、コマンドのサポートが組み込まれていない ControlRoutedCommand をフックする方法を示しています。 コマンドを複数のソースにフックする完全なサンプルについては、「Create a Custom RoutedCommand Sample サンプル」を参照してください。

Windows Presentation Foundation (WPF) には、アプリケーション プログラマが定期的に遭遇する一般的なコマンドのライブラリが用意されています。 コマンド ライブラリを構成するクラスは、ApplicationCommandsComponentCommandsNavigationCommandsMediaCommands、および EditingCommandsです。

これらのクラスを構成する静的 RoutedCommand オブジェクトには、コマンド ロジックは用意されていません。 コマンドのロジックは、CommandBindingを持つコマンドに関連付けられています。 WPF の多くのコントロールには、コマンド ライブラリの一部のコマンドのサポートが組み込まれています。 TextBoxは、たとえば、PasteCopyCutRedoUndoなどのアプリケーション編集コマンドの多くをサポートしています。 アプリケーション開発者は、これらのコントロールを操作するためにこれらのコマンドを取得するために特別な操作を行う必要はありません。 TextBox がコマンドの実行時にコマンド ターゲットである場合、コントロールに組み込まれている CommandBinding を使用してコマンドが処理されます。

Open コマンドのコマンド ソースとして Button を使用する方法を次に示します。 指定した CanExecuteRoutedEventHandlerCanExecuteRoutedEventHandlerRoutedCommandに関連付ける CommandBinding が作成されます。

まず、コマンド ソースが作成されます。 Button はコマンド ソースとして使用されます。

<Button Command="ApplicationCommands.Open" Name="MyButton"
        Height="50" Width="200">
  Open (KeyBindings: Ctrl+R, Ctrl+0)
</Button>
// Button used to invoke the command
Button CommandButton = new Button();
CommandButton.Command = ApplicationCommands.Open;
CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)";
MainStackPanel.Children.Add(CommandButton);
' Button used to invoke the command
Dim CommandButton As New Button()
CommandButton.Command = ApplicationCommands.Open
CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)"
MainStackPanel.Children.Add(CommandButton)

次に、ExecutedRoutedEventHandlerCanExecuteRoutedEventHandler が作成されます。 ExecutedRoutedEventHandler は、コマンドが実行されたことを示す MessageBox を開くだけです。 CanExecuteRoutedEventHandler は、CanExecute プロパティを trueに設定します。 通常、can execute ハンドラーは、コマンドが現在のコマンド ターゲットで実行できるかどうかを確認するために、より堅牢なチェックを実行します。


void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e)
{
    String command, targetobj;
    command = ((RoutedCommand)e.Command).Name;
    targetobj = ((FrameworkElement)target).Name;
    MessageBox.Show("The " + command +  " command has been invoked on target object " + targetobj);
}
void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}


Private Sub OpenCmdExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    Dim command, targetobj As String
    command = CType(e.Command, RoutedCommand).Name
    targetobj = CType(sender, FrameworkElement).Name
    MessageBox.Show("The " + command + " command has been invoked on target object " + targetobj)
End Sub
Private Sub OpenCmdCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
    e.CanExecute = True
End Sub

最後に、ルーティング イベント ハンドラーを Open コマンドに関連付ける CommandBinding が、アプリケーションのルート Window に作成されます。

<Window.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Open"
                  Executed="OpenCmdExecuted"
                  CanExecute="OpenCmdCanExecute"/>
</Window.CommandBindings>
// Creating CommandBinding and attaching an Executed and CanExecute handler
CommandBinding OpenCmdBinding = new CommandBinding(
    ApplicationCommands.Open,
    OpenCmdExecuted,
    OpenCmdCanExecute);

this.CommandBindings.Add(OpenCmdBinding);
' Creating CommandBinding and attaching an Executed and CanExecute handler
Dim OpenCmdBinding As New CommandBinding(ApplicationCommands.Open, AddressOf OpenCmdExecuted, AddressOf OpenCmdCanExecute)

Me.CommandBindings.Add(OpenCmdBinding)

関連項目