Note
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier les répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de changer de répertoire.
L’exemple suivant montre comment utiliser des commandes dans WPF (Windows Presentation Foundation). L’exemple montre comment associer un RoutedCommand à un Button, créer un CommandBindinget créer les gestionnaires d’événements qui implémentent le RoutedCommand. Pour plus d’informations sur les commandes, consultez la vue d’ensemble des commandes .
Exemple :
La première section du code crée l’interface utilisateur (UI), qui se compose d’un Button et d’un StackPanel, et crée un CommandBinding qui associe les gestionnaires de commandes au RoutedCommand.
La Command propriété du fichier Button est associée à la Close commande.
L'CommandBinding est ajouté à l'élément racine CommandBindingCollection. Les gestionnaires d'événements Executed et CanExecute sont attachés à cette liaison et associés à la commande Close.
Sans le CommandBinding, il n'y a pas de logique de commande, seulement un mécanisme pour appeler la commande. Lorsque le Button est cliqué, le PreviewExecutedRoutedEvent est déclenché sur la cible de commande, suivi par le ExecutedRoutedEvent. Ces événements parcourent l’arborescence d’éléments à la recherche d’un CommandBinding pour cette commande particulière. Il convient de noter que parce que RoutedEvent parcourent et passent à travers l’arbre des éléments, vous devez veiller à l’endroit où l’on place CommandBinding. Si le CommandBinding est situé sur un nœud frère de la cible de commande ou d'un autre nœud qui n'est pas sur l'itinéraire du RoutedEvent, le CommandBinding ne sera pas accessible.
<Window x:Class="WCSamples.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CloseCommand"
Name="RootWindow"
>
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Close"
Executed="CloseCommandHandler"
CanExecute="CanExecuteHandler"
/>
</Window.CommandBindings>
<StackPanel Name="MainStackPanel">
<Button Command="ApplicationCommands.Close"
Content="Close File" />
</StackPanel>
</Window>
// Create ui elements.
StackPanel CloseCmdStackPanel = new StackPanel();
Button CloseCmdButton = new Button();
CloseCmdStackPanel.Children.Add(CloseCmdButton);
// Set Button's properties.
CloseCmdButton.Content = "Close File";
CloseCmdButton.Command = ApplicationCommands.Close;
// Create the CommandBinding.
CommandBinding CloseCommandBinding = new CommandBinding(
ApplicationCommands.Close, CloseCommandHandler, CanExecuteHandler);
// Add the CommandBinding to the root Window.
RootWindow.CommandBindings.Add(CloseCommandBinding);
' Create ui elements.
Dim CloseCmdStackPanel As New StackPanel()
Dim CloseCmdButton As New Button()
CloseCmdStackPanel.Children.Add(CloseCmdButton)
' Set Button's properties.
CloseCmdButton.Content = "Close File"
CloseCmdButton.Command = ApplicationCommands.Close
' Create the CommandBinding.
Dim CloseCommandBinding As New CommandBinding(ApplicationCommands.Close, AddressOf CloseCommandHandler, AddressOf CanExecuteHandler)
' Add the CommandBinding to the root Window.
RootWindow.CommandBindings.Add(CloseCommandBinding)
La section suivante du code implémente les gestionnaires d’événements Executed et CanExecute.
Le Executed gestionnaire appelle une méthode pour fermer le fichier ouvert. Le CanExecute gestionnaire appelle une méthode pour déterminer si un fichier est ouvert. Si un fichier est ouvert, CanExecute est défini sur true; sinon, il est défini sur false.
// Executed event handler.
private void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
// Calls a method to close the file and release resources.
CloseFile();
}
// CanExecute event handler.
private void CanExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
{
// Call a method to determine if there is a file open.
// If there is a file open, then set CanExecute to true.
if (IsFileOpened())
{
e.CanExecute = true;
}
// if there is not a file open, then set CanExecute to false.
else
{
e.CanExecute = false;
}
}
' Executed event handler.
Private Sub CloseCommandHandler(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
' Calls a method to close the file and release resources.
CloseFile()
End Sub
' CanExecute event handler.
Private Sub CanExecuteHandler(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
' Call a method to determine if there is a file open.
' If there is a file open, then set CanExecute to true.
If IsFileOpened() Then
e.CanExecute = True
' if there is not a file open, then set CanExecute to false.
Else
e.CanExecute = False
End If
End Sub
Voir aussi
.NET Desktop feedback