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

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

Windows Presentation Foundation (WPF)提供了一个常见命令库,应用程序程序员经常遇到这些命令。 构成命令库的类包括:ApplicationCommandsComponentCommandsNavigationCommandsMediaCommandsEditingCommands

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

下面演示如何使用 Button 作为 Open 命令的命令源。 创建一个 CommandBinding,将指定的 CanExecuteRoutedEventHandlerCanExecuteRoutedEventHandlerRoutedCommand相关联。

首先,创建命令源。 将 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。 为了确定该命令是否可以在当前命令目标上执行,通常可执行处理程序会进行更全面的检查。


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

最后,在应用程序的根 Window 上创建一个 CommandBinding,该CommandBinding 将路由事件处理程序关联到 Open 命令。

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

另请参阅