方法 : コマンドをサポートしないコントロールにコマンドをフックする
コマンドのサポートが組み込まれていない Control に RoutedCommand をフックする方法を次の例に示します。 コマンドを複数のソースにフックするサンプル全体については、カスタム RoutedCommand の作成のサンプルを参照してください。
使用例
Windows Presentation Foundation (WPF) には、アプリケーション プログラマがよく使用する一般的なコマンドのライブラリが用意されています。 このコマンド ライブラリには、ApplicationCommands、ComponentCommands、NavigationCommands、MediaCommands、および EditingCommands クラスが含まれています。
これらのクラスを構成する静的な RoutedCommand オブジェクトには、コマンド ロジックが用意されていません。 コマンドのロジックは、CommandBinding でコマンドに関連付けられます。 WPF の多くのコントロールには、コマンド ライブリにある一部のコマンドのサポートが組み込まれています。 たとえば、TextBox は、Paste、Copy、Cut、Redo、Undo などの多くのアプリケーション編集コマンドをサポートしています。 アプリケーション開発者は、コントロールで使用するこれらのコマンドを取得するのに特別な作業を行う必要はありません。 TextBox がコマンド ターゲットである場合は、コマンドを実行すると、コントロールに組み込まれている CommandBinding を使用してコマンドを処理します。
Button を Open コマンドのコマンド ソースとして使用する方法を次に示します。 指定した 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
Dim CommandButton As New Button()
CommandButton.Command = ApplicationCommands.Open
CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)"
MainStackPanel.Children.Add(CommandButton)
// 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
Dim OpenCmdBinding As New CommandBinding(ApplicationCommands.Open, AddressOf OpenCmdExecuted, AddressOf OpenCmdCanExecute)
Me.CommandBindings.Add(OpenCmdBinding)
// Creating CommandBinding and attaching an Executed and CanExecute handler
CommandBinding OpenCmdBinding = new CommandBinding(
ApplicationCommands.Open,
OpenCmdExecuted,
OpenCmdCanExecute);
this.CommandBindings.Add(OpenCmdBinding);
参照
処理手順
方法 : コマンドをサポートするコントロールにコマンドをフックする