Commands in WPF
WPF Commands :
Command Model
It delegates events to the appropriate commands.
- It keeps the enabled state of a control synchronized with the state of the corresponding command.
Commands. A command represents an application task and keeps track of whether it can be executed. (But does not contain code);
Command bindings. Each command binding links a command to the related application logic, for a particular area of your user interface.
Command sources. A command source triggers a command. For example, a MenuItem and a Button can both be command sources. Clicking them executes the bound command.
Command targets. A command target is the element on which the command is being performed.
The ICommand Interface :
The heart of the WPF command model is the System.Windows.Input.ICommand interface, which defines how commands work.
This interface includes two methods and an event:
public interface ICommand
{
void Execute(object parameter);
bool CanExecute(object parameter);
event EventHandler CanExecuteChanged;
}
Execute() method would contain the application task logic
(for example, printing the document).
The
CanExecute() method returns the state of the command: true if it’s enabled and false if it’s disabled.
Both Execute() and CanExecute() accept an additional parameter object that we can use to pass along any extra information you need.
CanExecuteChanged event is raised when the state changes.
This is a signal to any controls using the command that they should call the CanExecute() method to check the command’s state.
The RoutedCommand Class
System.Windows.Input.RoutedCommand class implements ICommand .
All WPF commands are instances of RoutedCommand.
The RoutedCommand class adds extra infrastructure for event tunneling and bubbling.
public void Execute(object parameter, IInputElement target)
{...}
public bool CanExecute(object parameter, IInputElement target)
{...}
The RoutedUICommand Class
RoutedUICommand class derived from RoutedCommand.
RoutedUICommand is intended for commands with text that is displayed somewhere in the user interface (for example, the text of a menu item or the tooltip for a toolbar button).
The RoutedUICommand class adds a single property—Text—which is the display text for that command.
ApplicationCommands.
This class provides the common commands, including clipboard commands (such as Copy, Cut, and Paste) and document commands (such as New, Open, Save, SaveAs, Print, and so on).
NavigationCommands.
This class provides commands used for navigation, including some that are designed for page-based applications (such as BrowseBack, BrowseForward, and NextPage).
EditingCommands.
This class provides a document-editing commands, including commands for moving around (MoveToLineEnd, MoveLeftByWord, MoveUpByPage, and so on).
MediaCommands.
This class includes a set of commands for dealing with multimedia (such as Play, Pause, NextTrack, and IncreaseVolume).
Commands :
Programmatic control :
// Create the binding.
CommandBinding binding = new CommandBinding(ApplicationCommands.New);
// Attach the event handler.
binding.Executed += NewCommand_Executed;
// Register the binding.
this.CommandBindings.Add(binding);
private void NewCommand_Executed(object sender,
ExecutedRoutedEventArgs e)
{
MessageBox.Show("New command triggered by " + e.Source.ToString());
}
<Window x:Class="Commands.TestNewCommand"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestNewCommand">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.New"
Executed="NewCommand_Executed"></CommandBinding>
</Window.CommandBindings>
<StackPanel Margin="5">
<Button Padding="5" Command="ApplicationCommands.New">New</Button>
</StackPanel>
</Window>
Using Commands :
<Menu>
<MenuItem Header="File">
<MenuItem Command="New"></MenuItem>
</MenuItem>
</Menu>
<Button Command="New" Content="{x:Static ApplicationCommands.New}">
</Button>
Built-In Commands :
<ToolBarTray>
<ToolBar>
<Button Command="Cut"
CommandTarget="{Binding ElementName=txtDocument}">Cut</Button>
<Button Command="Copy"
CommandTarget="{Binding ElementName=txtDocument}">Copy</Button>
<Button Command="Paste"
CommandTarget="{Binding ElementName=txtDocument}">Paste</Button>
</ToolBar>
</ToolBarTray>
Custom Commands :
public class DataCommands<
{
private static RoutedUICommand requery;
static DataCommands()
{
// Initialize the command.
InputGestureCollection inputs = new InputGestureCollection();
inputs.Add(new KeyGesture(Key.R, ModifierKeys.Control, "Ctrl+R"));
requery = new RoutedUICommand(
"Requery", "Requery", typeof(DataCommands), inputs);
}
public static RoutedUICommand Requery
{
get { return requery; }
}
After defining custom command, use it
<window…….>
<Window.CommandBindings>
<CommandBinding Command="local:DataCommands.Requery"
Executed="RequeryCommand_Executed"></CommandBinding>
</Window.CommandBindings>
<Button Margin="5" Command="local:DataCommands.Requery">Requery
</Button>
</Window>
To complete this example, you simply need to implement the RequeryCommand_Executed() event handler in your code.