Partager via


Guide pratique pour créer une commande routée

Cet exemple montre comment créer un RoutedCommand personnalisé et comment implémenter la commande personnalisée en créant un ExecutedRoutedEventHandler et un CanExecuteRoutedEventHandler et en les attachant à un CommandBinding. Pour plus d’informations sur les commandes, consultez la vue d’ensemble des commandes .

Exemple :

La première étape de la création d’un RoutedCommand consiste à définir la commande et à l’instancier.

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

Pour utiliser la commande dans une application, les gestionnaires d’événements qui définissent ce que doit faire la commande doivent être créés

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

Ensuite, une CommandBinding est créée qui associe la commande aux gestionnaires d’événements. Le CommandBinding est créé sur un objet spécifique. Cet objet définit l’étendue du CommandBinding dans l’arborescence d’éléments

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

La dernière étape consiste à appeler la commande. Une façon d’appeler une commande consiste à l’associer à un ICommandSource, comme un 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

Lorsque le bouton est cliqué, la méthode Execute sur le RoutedCommand personnalisé est appelée. Le RoutedCommand déclenche les événements routés PreviewExecuted et Executed. Ces événements parcourent l’arborescence d’éléments à la recherche d’une CommandBinding pour cette commande particulière. Si une CommandBinding est trouvée, la ExecutedRoutedEventHandler associée à CommandBinding est appelée.

Voir aussi