Dieser Artikel wurde manuell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. |
Übersetzung
Original
|
CommandBinding-Klasse
Bindet einen RoutedCommand an Ereignishandler, die den Befehl implementieren
Assembly: PresentationCore (in PresentationCore.dll)
XMLNS für XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Der CommandBinding-Typ macht die folgenden Member verfügbar.
| Name | Beschreibung | |
|---|---|---|
|
CommandBinding() | Initialisiert eine neue Instanz der CommandBinding-Klasse. |
|
CommandBinding(ICommand) | Initialisiert eine neue Instanz der CommandBinding-Klasse mit dem angegebenen ICommand-Wert. |
|
CommandBinding(ICommand, ExecutedRoutedEventHandler) | Initialisiert eine neue Instanz der CommandBinding-Klasse mit dem angegebenen ICommand und dem angegebenen Executed-Ereignishandler. |
|
CommandBinding(ICommand, ExecutedRoutedEventHandler, CanExecuteRoutedEventHandler) | Initialisiert eine neue Instanz der CommandBinding-Klasse mit dem angegebenen ICommand sowie dem angegebenen Executed-Ereignishandler und dem angegebenen CanExecute-Ereignishandler. |
| Name | Beschreibung | |
|---|---|---|
|
Command | Ruft die ICommand ab, die diesem CommandBinding zugeordnet sind, oder legt diese fest. |
| Name | Beschreibung | |
|---|---|---|
|
Equals(Object) | Bestimmt, ob das angegebene Object und das aktuelle Object gleich sind. (Von Object geerbt.) |
|
Finalize | Gibt einem Objekt Gelegenheit zu dem Versuch, Ressourcen freizugeben und andere Bereinigungen durchzuführen, bevor es von der automatische Speicherbereinigung freigegeben wird. (Von Object geerbt.) |
|
GetHashCode | Fungiert als Hashfunktion für einen bestimmten Typ. (Von Object geerbt.) |
|
GetType | Ruft den Type der aktuellen Instanz ab. (Von Object geerbt.) |
|
MemberwiseClone | Erstellt eine flache Kopie des aktuellen Object. (Von Object geerbt.) |
|
ToString | Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt. (Von Object geerbt.) |
| Name | Beschreibung | |
|---|---|---|
|
CanExecute | Tritt auf, wenn der Befehl, der mit der CommandBinding verknüpft ist, prüft, ob der Befehl für das Befehlsziel ausgeführt werden kann. |
|
Executed | Tritt auf, wenn der mit dieser CommandBinding verknüpfte Befehl ausgeführt wird. |
|
PreviewCanExecute | Tritt auf, wenn der Befehl, der mit der CommandBinding verknüpft ist, prüft, ob der Befehl für das aktuelle Befehlsziel ausgeführt werden kann. |
|
PreviewExecuted | Tritt auf, wenn der mit dieser CommandBinding verknüpfte Befehl ausgeführt wird. |
Eine CommandBinding ordnet einem Befehl das PreviewExecuted/Executed-Ereignis und das PreviewCanExecute/CanExecute-Ereignis zu, die den Status des Befehls implementieren und ermitteln.
Wenn die Execute-Methode oder die CanExecute-Methode eines RoutedCommand aufgerufen werden, wird das PreviewExecuted/Executed-Ereignis oder das PreviewCanExecute/CanExecute-Ereignis für das Befehlsziel ausgelöst. Wenn das Befehlsziel eine CommandBinding für den Befehl aufweist, werden die entsprechenden Handler aufgerufen. Wenn das Befehlsziel keine CommandBinding für den Befehl aufweist, werden die Ereignisse in der Elementstruktur weitergeleitet, bis ein Element mit einer CommandBinding gefunden wird.
Eine CommandBinding kann nur eingeschränkt verwendet werden, wenn der ICommand keine RoutedCommand darstellt. Dies liegt daran, dass eine CommandBinding den Befehl an den ExecutedRoutedEventHandler und den CanExecuteRoutedEventHandler bindet, die das Executed-Routingereignis und das CanExecute-Routingereignis überwachen. Diese werden ausgelöst, wenn die Execute-Methode und die CanExecute-Methode des RoutedCommand aufgerufen werden.
The following example demonstrates how to use commanding in Windows Presentation Foundation (WPF). The example shows how to associate a RoutedCommand to a Button, create a CommandBinding, and create the event handlers which implement the RoutedCommand. For more information on commanding, see the Befehlsübersicht.
The first section of code creates the user interface (UI), which consists of a Button and a StackPanel, and creates a CommandBinding that associates the command handlers with the RoutedCommand.
The Command property of the Button is associated with the Close command.
The CommandBinding is added to the CommandBindingCollection of the root Window. The Executed and CanExecute event handlers are attached to this binding and associated with the Close command.
Without the CommandBinding there is no command logic, only a mechanism to invoke the command. When the Button is clicked, the PreviewExecuted RoutedEvent is raised on the command target followed by the Executed RoutedEvent. These events traverse the element tree looking for a CommandBinding for that particular command. It is worth noting that because RoutedEvent tunnel and bubble through the element tree, care must be taken in where the CommandBinding is put. If the CommandBinding is on a sibling of the command target or another node that is not on the route of the RoutedEvent, the CommandBinding will not be accessed.
<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);
The next section of code implements the Executed and CanExecute event handlers.
The Executed handler calls a method to close the open file. The CanExecute handler calls a method to determine whether a file is open. If a file is open, CanExecute is set to true; otherwise, it is set to 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; } }
Windows 7, Windows Vista SP1 oder höher, Windows XP SP3, Windows Server 2008 (Server Core wird nicht unterstützt), Windows Server 2008 R2 (Server Core wird mit SP1 oder höher unterstützt), Windows Server 2003 SP2
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.