
Visual Basic (Declaration) | |
---|---|
Public Class CommandBinding |
Visual Basic (Usage) | ![]() |
---|---|
Dim instance As CommandBinding |
C# | |
---|---|
public class CommandBinding |
Delphi | |
---|---|
public class CommandBinding |
JScript | |
---|---|
public class CommandBinding |
Managed Extensions for C++ | |
---|---|
public __gc class CommandBinding |
C++/CLI | |
---|---|
public ref class CommandBinding |
A CommandBinding associates a command with the PreviewExecuted / Executed and PreviewCanExecute / CanExecute events which implement and determine the status of the command.
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 CommandBinding for the command, the appropriate handlers are called. If the command target does not have a CommandBinding for the command, the events are routed through the element tree until an element that has a CommandBinding is found.
The following code example illustrates the big picture of these processes and shows how the routed command and command binding can be easily defined in XAML.
XAML | ![]() |
---|---|
<Grid x:Name="LayoutRoot"> <Intersoft:CommandManager.CommandBindings> <Intersoft:CommandBindingCollection> <Intersoft:CommandBinding Command="Commands:EditingCommands.Cut" CanExecute="CutCmdCanExecute" Executed="CutExecuted"/> </Intersoft:CommandBindingCollection> </Intersoft:CommandManager.CommandBindings> <Intersoft:DockPanel Name="dockPanel1" FillChildMode="Custom"> <Intersoft:UXToolBar Name="toolBar1" Intersoft:DockPanel.Dock="Top"> <Intersoft:UXToolBarButton Name="btn_Cut" Command="Commands:EditingCommands.Cut" DisplayMode="Image" Icon="../Images/CutHS.png" ToolTipService.ToolTip="Cut"/> </Intersoft:UXToolBar> <Intersoft:UXTextBox Name="textBox1" Text="Type your text here..." Intersoft:DockPanel.IsFillElement="True" /> </Intersoft:DockPanel> </Grid> |
The following code example shows the event handler for the CanExecute and Executed routed event for the CommandBinding associated to the Cut command.
C# | ![]() |
---|---|
using System.Windows; using ClientUIApplication_Docs.Commands; using Intersoft.Client.Framework.Input; using Intersoft.Client.UI.Navigation; namespace ClientUIApplication_Docs.RoutedCommands { public partial class CommandOverview : UXPage { public CommandOverview() { // It is important to initialize the static EditingCommands class // before InitializeComponent is called EditingCommands.Initialize(); InitializeComponent(); } private void CutCmdCanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } private void CutExecuted(object sender, ExecutedRoutedEventArgs e) { string command = ((RoutedCommand)e.Command).Name; string target = ((FrameworkElement)e.OriginalSource).Name; MessageBox.Show("The " + command + " command has been invoked on target object " + target); } } } |
For a more complete version of this example, see How-to: Implement a RoutedCommand. To learn more about commanding, see Commanding Overview.
Intersoft.Client.Framework.Input.CommandBinding
Intersoft.Client.Framework.Input.HybridCommandBinding
Target Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family