Notes
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier des répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de modifier des répertoires.
Cet exemple montre comment créer une RoutedCommand personnalisée et l'implémenter en créant un ExecutedRoutedEventHandler et un CanExecuteRoutedEventHandler et en les joignant à une CommandBinding. Pour plus d'informations sur l'exécution des commandes, consultez Vue d'ensemble des commandes.
Exemple
La première étape de la création d'une RoutedCommand consiste à définir la commande et à l'instancier.
Public Shared CustomRoutedCommand As New RoutedCommand()
public static RoutedCommand CustomRoutedCommand = new RoutedCommand();
Pour utiliser la commande dans une application, vous devez créer les gestionnaires d'événements qui définissent le rôle de cette commande.
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;
}
}
Une CommandBinding associant la commande aux gestionnaires d'événements est ensuite créée. La CommandBinding est créée sur un objet spécifique. Cet objet définit la portée de la CommandBinding dans l'arborescence d'éléments
<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);
La dernière étape consiste à appeler la commande. Pour ce faire, vous pouvez associer la commande à une ICommandSource, telle qu'un Button.
<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;
Lorsque vous cliquez sur le bouton, la méthode Execute sur la RoutedCommand personnalisée est appelée. La 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, le ExecutedRoutedEventHandler associé à cette CommandBinding est appelé.