Procedura: attivare un comando
Nell'esempio seguente viene illustrato come usare i comandi in Windows Presentation Foundation (WPF). Nell'esempio viene illustrato come associare un RoutedCommand oggetto a un Buttonoggetto , creare un CommandBindingoggetto e creare i gestori eventi che implementano l'oggetto RoutedCommand. Per altre informazioni sui comandi, vedere Panoramica dei comandi.
Esempio
La prima sezione del codice crea l'interfaccia utente , costituita da e ButtonStackPanelcrea un oggetto CommandBinding che associa i gestori comandi a RoutedCommand.
La Command proprietà di Button è associata al Close comando .
L'oggetto CommandBinding viene aggiunto all'oggetto CommandBindingCollection della radice Window. I Executed gestori eventi e CanExecute sono collegati a questa associazione e associati al Close comando .
Senza la logica di CommandBinding comando non esiste un meccanismo per richiamare il comando. Quando si fa clic su , l'oggetto viene generato nella destinazione del comando seguito da ExecutedRoutedEvent.ButtonPreviewExecutedRoutedEvent Questi eventi attraversano l'albero degli elementi alla ricerca di un oggetto CommandBinding per quel particolare comando. Vale la pena notare che perché RoutedEvent tunnel e bolle attraverso l'albero degli elementi, è necessario prestare attenzione in dove viene messo .CommandBinding Se si CommandBinding trova in un elemento di pari livello della destinazione del comando o di un altro nodo che non si trova nella route di RoutedEvent, non verrà eseguito l'accesso CommandBinding a .
<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 sezione successiva del codice implementa i Executed gestori eventi e CanExecute .
Il Executed gestore chiama un metodo per chiudere il file aperto. Il CanExecute gestore chiama un metodo per determinare se un file è aperto. Se un file è aperto, CanExecute è impostato su true
; in caso contrario, è impostato su 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
Vedi anche
.NET Desktop feedback
Commenti e suggerimenti
https://aka.ms/ContentUserFeedback.
Presto disponibile: Nel corso del 2024 verranno gradualmente disattivati i problemi di GitHub come meccanismo di feedback per il contenuto e ciò verrà sostituito con un nuovo sistema di feedback. Per altre informazioni, vedereInvia e visualizza il feedback per