CSharpFeeds - All your C# feeds in one place.

Sponsors

Tuesday, May 15, 2007

How to Add Commands to Custom WPF Control

by Keyvan Nayyeri via Keyvan Nayyeri on 5/15/2007 6:08:46 PM

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:

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

email it!bookmark it!digg it!

Original Post: How to Add Commands to Custom WPF Control

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us