如何:创建 RoutedCommand

此示例演示如何创建自定义 RoutedCommand 以及如何通过创建 ExecutedRoutedEventHandlerCanExecuteRoutedEventHandler 并将其附加到 CommandBinding来实现自定义命令。 有关命令的详细信息,请参阅 命令概述

创建 RoutedCommand 的第一步是定义命令并对其进行实例化。

public static RoutedCommand CustomRoutedCommand = new RoutedCommand();
Public Shared CustomRoutedCommand As New RoutedCommand()

为了在应用程序中使用命令,必须创建定义命令功能的事件处理程序。

private void ExecutedCustomCommand(object sender,
    ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Custom Command Executed");
}
Private Sub ExecutedCustomCommand(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    MessageBox.Show("Custom Command Executed")
End Sub
// CanExecuteRoutedEventHandler that only returns true if
// the source is a control.
private void CanExecuteCustomCommand(object sender,
    CanExecuteRoutedEventArgs e)
{
    Control target = e.Source as Control;

    if(target != null)
    {
        e.CanExecute = true;
    }
    else
    {
        e.CanExecute = false;
    }
}
' CanExecuteRoutedEventHandler that only returns true if
' the source is a control.
Private Sub CanExecuteCustomCommand(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
    Dim target As Control = TryCast(e.Source, Control)

    If target IsNot Nothing Then
        e.CanExecute = True
    Else
        e.CanExecute = False
    End If
End Sub

接下来,将创建一个 CommandBinding,用于将命令与事件处理程序相关联。 CommandBinding 是在特定对象上创建的。 此对象定义元素树中 CommandBinding 的范围

<Window x:Class="SDKSamples.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom="clr-namespace:SDKSamples"
    Height="600" Width="800"
    >
  <Window.CommandBindings>
    <CommandBinding Command="{x:Static custom:Window1.CustomRoutedCommand}"
                    Executed="ExecutedCustomCommand"
                    CanExecute="CanExecuteCustomCommand" />
  </Window.CommandBindings>
CommandBinding customCommandBinding = new CommandBinding(
    CustomRoutedCommand, ExecutedCustomCommand, CanExecuteCustomCommand);

// attach CommandBinding to root window
this.CommandBindings.Add(customCommandBinding);
Dim customCommandBinding As New CommandBinding(CustomRoutedCommand, AddressOf ExecutedCustomCommand, AddressOf CanExecuteCustomCommand)

' attach CommandBinding to root window
Me.CommandBindings.Add(customCommandBinding)

最后一步是调用命令。 调用命令的一种方法是将其与 ICommandSource相关联,例如 Button

<StackPanel>
  <Button Command="{x:Static custom:Window1.CustomRoutedCommand}"
          Content="CustomRoutedCommand"/>
</StackPanel>
// create the ui
StackPanel CustomCommandStackPanel = new StackPanel();
Button CustomCommandButton = new Button();
CustomCommandStackPanel.Children.Add(CustomCommandButton);

CustomCommandButton.Command = CustomRoutedCommand;
' create the ui
Dim CustomCommandStackPanel As New StackPanel()
Dim CustomCommandButton As New Button()
CustomCommandStackPanel.Children.Add(CustomCommandButton)

CustomCommandButton.Command = CustomRoutedCommand

单击按钮时,将调用自定义 RoutedCommand 上的 Execute 方法。 RoutedCommand 引发 PreviewExecutedExecuted 路由事件。 这些事件会遍历元素树,查找此特定命令的 CommandBinding。 如果找到 CommandBinding,则调用与 CommandBinding 关联的 ExecutedRoutedEventHandler

另请参阅