如何:将命令挂钩到不支持命令的控件

以下示例演示如何将 RoutedCommand 挂钩到不含对该命令的内置支持的 Control。 有关将命令挂钩到多个源的完整示例,请参阅创建自定义 RoutedCommand 示例示例。

示例

Windows Presentation Foundation (WPF) 提供了应用程序程序员经常遇到的常见命令库。 构成命令库的类为:ApplicationCommandsComponentCommandsNavigationCommandsMediaCommandsEditingCommands

构成这些类的静态 RoutedCommand 对象不提供命令逻辑。 命令的逻辑通过 CommandBinding 与命令相关联。 WPF 中的许多控件都为命令库中的某些命令提供内置支持。 例如,TextBox 支持许多应用程序编辑命令,例如 PasteCopyCutRedoUndo。 应用程序开发人员不必执行任何特殊操作即可使命令适用于这些控件。 如果命令执行时,TextBox 是命令目标,它将使用内置于控件中的 CommandBinding 处理该命令。

下面演示了如何将 Button 用作 Open 命令的命令源。 创建一个将指定的 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)

接下来,创建 ExecutedRoutedEventHandlerCanExecuteRoutedEventHandlerExecutedRoutedEventHandler 只需打开 MessageBox 即可表示已执行该命令。 CanExecuteRoutedEventHandlerCanExecute 属性设置为 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 命令相关联的应用程序的根 Window 上创建 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);
' Creating CommandBinding and attaching an Executed and CanExecute handler
Dim OpenCmdBinding As New CommandBinding(ApplicationCommands.Open, AddressOf OpenCmdExecuted, AddressOf OpenCmdCanExecute)

Me.CommandBindings.Add(OpenCmdBinding)

另请参阅