共用方式為


如何:建立 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

按一下按鈕時,即會在自訂 Execute 上呼叫 RoutedCommand 方法。 RoutedCommand 會引發 PreviewExecutedExecuted 路由事件。 這些事件會周遊元素樹狀結構,尋找此特定命令的 CommandBinding。 如果找到 CommandBinding,就會呼叫與 ExecutedRoutedEventHandler 相關聯的 CommandBinding

另請參閱