Freigeben über


Gewusst wie: Erstellen eines RoutedCommand

In diesem Beispiel wird gezeigt, wie ein benutzerdefinierter RoutedCommand erstellt und durch das Erstellen eines ExecutedRoutedEventHandler und eines CanExecuteRoutedEventHandler und das Anfügen von beiden an eine CommandBinding implementiert wird. Weitere Informationen über Befehle finden Sie unter Befehlsübersicht.

Beispiel

Der erste Schritt bei der Erstellung eines RoutedCommand besteht darin, den Befehl zu definieren und zu instanziieren.

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

Um den Befehl in einer Anwendung verwenden zu können, müssen Ereignishandler erstellt werden, die die Funktionen des Befehls definieren.

        Private Sub ExecutedCustomCommand(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
            MessageBox.Show("Custom Command Executed")
        End Sub
private void ExecutedCustomCommand(object sender,
    ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Custom Command Executed");
}
        ' 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
// 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;
    }
}

Anschließend wird eine CommandBinding erstellt, die den Befehl den Ereignishandlern zuordnet. Die CommandBinding wird auf einem bestimmten Objekt erstellt. Dieses Objekt definiert den Bereich der CommandBinding in der Elementstruktur.

<Window x:Class="SDKSamples.Window1"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://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>
            Dim customCommandBinding As New CommandBinding(CustomRoutedCommand, AddressOf ExecutedCustomCommand, AddressOf CanExecuteCustomCommand)

            ' attach CommandBinding to root window
            Me.CommandBindings.Add(customCommandBinding)
CommandBinding customCommandBinding = new CommandBinding(
    CustomRoutedCommand, ExecutedCustomCommand, CanExecuteCustomCommand);

// attach CommandBinding to root window
this.CommandBindings.Add(customCommandBinding);

Im letzten Schritt wird der Befehl aufgerufen. Eine Möglichkeit, einen Befehl aufzurufen, besteht darin, ihn einer ICommandSource wie einem Button zuzuordnen.

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

            CustomCommandButton.Command = CustomRoutedCommand
// create the ui
StackPanel CustomCommandStackPanel = new StackPanel();
Button CustomCommandButton = new Button();
CustomCommandStackPanel.Children.Add(CustomCommandButton);

CustomCommandButton.Command = CustomRoutedCommand;

Wenn auf die Schaltfläche geklickt wird, wird die Execute-Methode auf dem benutzerdefinierten RoutedCommand aufgerufen. RoutedCommand löst die Routingereignisse PreviewExecuted und Executed aus. Diese Ereignisse durchlaufen die Elementstruktur auf der Suche nach einer CommandBinding für diesen speziellen Befehl. Wenn eine CommandBinding gefunden wird, wird der ExecutedRoutedEventHandler aufgerufen, der CommandBinding zugeordnet ist.

Siehe auch

Referenz

RoutedCommand

Konzepte

Befehlsübersicht