Partager via


Comment : raccorder une commande à un contrôle sans prise en charge de commande

L’exemple suivant montre comment raccorder RoutedCommand à un Control qui n’a pas de prise en charge intégrée de la commande. Pour obtenir un exemple complet qui raccorde des commandes à plusieurs sources, consultez l’exemple Créer un exemple RoutedCommand personnalisé.

Exemple

Windows Presentation Foundation (WPF) fournit une bibliothèque de commandes courantes que les programmeurs d’applications rencontrent régulièrement. Les classes qui constituent la bibliothèque de commandes sont : ApplicationCommands, ComponentCommands, NavigationCommands, MediaCommands et EditingCommands.

Les objets statiques RoutedCommand qui composent ces classes ne fournissent pas de logique de commande. La logique de la commande est associée à la commande avec CommandBinding. De nombreux contrôles dans WPF prennent en charge certaines des commandes de la bibliothèque de commandes. TextBox, par exemple, prend en charge de nombreuses commandes d’édition de l’application, par exemple Paste, Copy, Cut, Redo et Undo. Le développeur d’application n’a aucune action spéciale à effectuer pour que ces commandes fonctionnent avec ces contrôles. Si TextBox est la cible de commande au moment de l’exécution de la commande, il gère la commande à l’aide de CommandBinding, qui est intégré au contrôle.

L’exemple suivant montre comment utiliser Button en tant que source de commande pour la commande Open. Un CommandBinding est créé pour associer le CanExecuteRoutedEventHandler spécifié et CanExecuteRoutedEventHandler à RoutedCommand.

Tout d’abord, la source de commande est créée. Button est utilisé comme source de commande.

<Button Command="ApplicationCommands.Open" Name="MyButton"
        Height="50" Width="200">
  Open (KeyBindings: Ctrl+R, Ctrl+0)
</Button>
// Button used to invoke the command
Button CommandButton = new Button();
CommandButton.Command = ApplicationCommands.Open;
CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)";
MainStackPanel.Children.Add(CommandButton);
' Button used to invoke the command
Dim CommandButton As New Button()
CommandButton.Command = ApplicationCommands.Open
CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)"
MainStackPanel.Children.Add(CommandButton)

ExecutedRoutedEventHandler et CanExecuteRoutedEventHandler sont ensuite créés. ExecutedRoutedEventHandler ouvre simplement MessageBox pour indiquer que la commande a été exécutée. CanExecuteRoutedEventHandler affecte à la propriété CanExecute la valeur true. Normalement, le gestionnaire d’exécution effectue des vérifications plus robustes pour déterminer si la commande peut s’exécuter sur la cible de commande actuelle.


void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e)
{
    String command, targetobj;
    command = ((RoutedCommand)e.Command).Name;
    targetobj = ((FrameworkElement)target).Name;
    MessageBox.Show("The " + command +  " command has been invoked on target object " + targetobj);
}
void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}


Private Sub OpenCmdExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    Dim command, targetobj As String
    command = CType(e.Command, RoutedCommand).Name
    targetobj = CType(sender, FrameworkElement).Name
    MessageBox.Show("The " + command + " command has been invoked on target object " + targetobj)
End Sub
Private Sub OpenCmdCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
    e.CanExecute = True
End Sub

Enfin, CommandBinding est créé à la racine Window de l’application qui associe les gestionnaires d’événements routés à la commande Open.

<Window.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Open"
                  Executed="OpenCmdExecuted"
                  CanExecute="OpenCmdCanExecute"/>
</Window.CommandBindings>
// Creating CommandBinding and attaching an Executed and CanExecute handler
CommandBinding OpenCmdBinding = new CommandBinding(
    ApplicationCommands.Open,
    OpenCmdExecuted,
    OpenCmdCanExecute);

this.CommandBindings.Add(OpenCmdBinding);
' Creating CommandBinding and attaching an Executed and CanExecute handler
Dim OpenCmdBinding As New CommandBinding(ApplicationCommands.Open, AddressOf OpenCmdExecuted, AddressOf OpenCmdCanExecute)

Me.CommandBindings.Add(OpenCmdBinding)

Voir aussi