How to Add Commands to Custom WPF Control
Back in Beta days of .NET Framework 3.0, I wrote a blog post about commands in WPF and how easy it is to add common commands to your WPF menus and create custom commands to use them.
On the other hand, so far I've written some posts about writing a custom WPF control:
- How to Create a Custom WPF Control
- How to Add Properties to Custom WPF Control
- How to Add Events to Custom WPF Control
But now and before writing a separate post to show a sample custom WPF control, I want to cover another topic about adding commands to custom WPF controls.
Adding a command to a custom control is so easy. The main part is registering the command in static constructor of your custom control. There are two kinds of commands that you can add to your custom controls:
- New Command Type: you can define a completely new command type and handle it in your control.
- Existing Command: you can handle an existing command type (i.e. built-in command types in WPF) in your custom control.
Therefore I split my post into two parts and cover how to add each of these two kinds to your controls.
New Command Type
In this case you must register your command in static constructor of control. This RoutedCommand is a static public member of class and you need to set it to a correct value. In order to do this you can create a InputGestureCollection object in static constructor and add an input to this collection. In my sample, I add a KeyGesture to inputs which yields that this command runs every time that user press Ctrl + M. After adding an input to InputGestureCollection, you can assign a new instance of RoutedCommand object to your command. This new object can be created by passing a string name, type of your custom control and your newly added InputGestureCollection object to its constructor. After passing these steps your new command type is registered.
public class MyControl : ItemsControl
{
public static RoutedCommand myCommand;
static MyControl()
{
InputGestureCollection myInputs = new InputGestureCollection();
myInputs.Add(new KeyGesture(Key.M, ModifierKeys.Control));
myCommand = new RoutedCommand("CustomCommand",
typeof(MyControl), myInputs);
}
}
But story has not ended yet. You still need to create a binding for your command to be able to use it. This step is common with existing commands so I'll describe it in next section.
Existing Command
Whether you're defining a new command type or using an existing command, should declare a CommandBinding object for your command to bind it to its handler. After declaring this CommandBinding object, you must register it using CommandManager.RegisterClassCommandBinding() method. Note that your handler must be static. In below code I bind existing Save command in WPF to my own CommandHandler handler to show a MessageBox whenever this command is running.
public class MyControl : ItemsControl
{
static MyControl()
{
CommandBinding binding =
new CommandBinding(ApplicationCommands.Save, CommandHandler);
CommandManager.RegisterClassCommandBinding(typeof(MyControl), binding);
}
private static void CommandHandler(object target, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Command Handled!");
}
}
And finally in last example I modify the code from previous section to create a binding for my new command type.
public class MyControl : ItemsControl
{
public static RoutedCommand myCommand;
static MyControl()
{
InputGestureCollection myInputs = new InputGestureCollection();
myInputs.Add(new KeyGesture(Key.M, ModifierKeys.Control));
myCommand = new RoutedCommand("CustomCommand",
typeof(MyControl), myInputs);
CommandBinding binding = new CommandBinding();
binding.Command = myCommand;
binding.Executed += new ExecutedRoutedEventHandler(binding_Executed);
CommandManager.RegisterClassCommandBinding(typeof(MyControl), binding);
}
private static void binding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Command Handled!");
}
}
Now playing: Ricky Martin - Life
[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.
6 Comments : 05.15.07
Feedbacks
Thanks for the article. Manage to add my command in the WPF Custom Control Gauge I downloaded from <a href="http://www.nextwavesoft.com">http://www.nextwavesoft.com</a>
#6
Exam 70-502 Microsoft .NET Framework 3.5 Windows Presentation Foundation Study Links - Part 2 | One .Net Way
08.11.2008 @ 4:40 AM
Pingback from Exam 70-502 Microsoft .NET Framework 3.5 Windows Presentation Foundation Study Links - Part 2 | One .Net Way

#1
DotNetKicks.com
05.15.2007 @ 11:13 AM