Binds a RoutedCommand to the event handlers that implement the command.
Assembly: PresentationCore (in PresentationCore.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Thetype exposes the following members.
|CommandBinding()||Initializes a new instance of the class.|
|CommandBinding(ICommand)||Initializes a new instance of the class by using the specified ICommand.|
|CommandBinding(ICommand, ExecutedRoutedEventHandler)||Initializes a new instance of the class by using the specified ICommand and the specified Executed event handler.|
|CommandBinding(ICommand, ExecutedRoutedEventHandler, CanExecuteRoutedEventHandler)||Initializes a new instance of the class by using the specified ICommand and the specified Executed and CanExecute event handlers.|
|Equals(Object)||Determines whether the specified object is equal to the current object. (Inherited from Object.)|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)|
|GetHashCode||Serves as the default hash function. (Inherited from Object.)|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
|CanExecute||Occurs when the command associated with this initiates a check to determine whether the command can be executed on the command target.|
|Executed||Occurs when the command associated with this executes.|
|PreviewCanExecute||Occurs when the command associated with this initiates a check to determine whether the command can be executed on the current command target.|
|PreviewExecuted||Occurs when the command associated with this executes.|
When the Execute or CanExecute method of a RoutedCommand is called, the PreviewExecuted/Executed or the PreviewCanExecute/CanExecute events are raised on the command target. If the command target has a for the command, the appropriate handlers are called. If the command target does not have a for the command, the events are routed through the element tree until an element that has a is found.
A has limited use with an ICommand that is not a RoutedCommand. This is because a binds the command to the ExecutedRoutedEventHandler and the CanExecuteRoutedEventHandler which listen to the Executed and CanExecute routed events which are raised when the Execute and CanExecute method of the RoutedCommand are called.
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 , and create the event handlers which implement the RoutedCommand. For more information on commanding, see the Commanding Overview.
Without the 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 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 is put. If the is on a sibling of the command target or another node that is not on the route of the RoutedEvent, the 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>
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.
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.