次の方法で共有


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

更新 : 2007 年 11 月

コマンドのサポートが組み込まれていない ControlRoutedCommand をフックする方法を次の例に示します。コマンドを複数のソースに関連付けるサンプル全体については、「カスタム RoutedCommand の作成のサンプル」を参照してください。

使用例

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

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

ButtonOpen コマンドのコマンド ソースとして使用する方法を次に示します。指定した CanExecuteRoutedEventHandler と、RoutedCommand を持つ CanExecuteRoutedEventHandler を関連付ける 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);

次に、ExecutedRoutedEventHandler および CanExecuteRoutedEventHandler を作成します。ExecutedRoutedEventHandler は、コマンドが実行されたことを知らせるために、MessageBox を開きます。CanExecuteRoutedEventHandler は、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

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;
}

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

<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);

参照

処理手順

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

概念

コマンド実行の概要