Gewusst wie: Aktivieren eines Befehls

Im folgenden Beispiel wird veranschaulicht, wie Sie Befehle in Windows Presentation Foundation (WPF) verwenden. Das Beispiel zeigt, wie Sie RoutedCommand mit Button verknüpfen, CommandBinding erstellen und die Ereignishandler erstellen, die das RoutedCommand integrieren. Weitere Informationen über Befehle finden Sie unter Befehlsübersicht.

Beispiel

Der erste Abschnitt des Codes erstellt die Benutzeroberfläche (UI), die aus Button und StackPanel besteht, und erstellt CommandBinding, das die Befehlshandler mit den RoutedCommand verbindet.

Die Command-Eigenschaft des Button, das diesem Close-Befehl zugeordnet ist.

CommandBinding wird dem CommandBindingCollection des Window-Stamms hinzugefügt. Die Executed- und CanExecute-Ereignishandler werden dieser Bindung und dem Close-Befehl zugeordnet.

Ohne die CommandBinding-Befehlslogik gibt es nur einen Mechanismus zum Aufrufen des Befehls. Wenn die Button angeklickt wird, wird die PreviewExecutedRoutedEvent auf dem Befehlsziel ausgelöst, gefolgt von der ExecutedRoutedEvent. Diese Ereignisse durchlaufen den Elementbaum auf der Suche nach einem CommandBinding für den bestimmten Befehl. Da RoutedEvent-Tunnel und -Bubble durch den Elementbaum verlaufen, müssen Sie darauf achten, wo CommandBinding platziert wird. Wenn sich CommandBinding auf einem Geschwisterknoten des Befehlsziels oder einem anderen Knoten befindet, der nicht auf der Route des RoutedEvent liegt, wird nicht auf CommandBinding zugegriffen.

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

Der nächste Codeabschnitt implementiert die Executed- und CanExecute-Ereignishandler.

Der Executed-Handler ruft eine Methode auf, um die geöffnete Datei zu schließen. Der CanExecute-Handler ruft eine Methode auf, um zu bestimmen, ob eine Datei geöffnet ist. Wenn eine Datei geöffnet ist, wird CanExecute auf true festegelet; andernfalls ist sie auf false festgelegt.

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

Weitere Informationen