Posted by: Zeeshan Amjad | February 11, 2010

Creating Routed Commands


We have already seen example of implementing ICommand interface here and here. WPF already define one class which already implement ICommand interface. That class is RoutedCommand. Here is a class diagram of RoutedCommand.

RoutedCommand

There is one more class inherited by RoutedCommand named RoutedUICommand. RoutedUICommand added on Text property. We can also define the input gesture when defining the new command. There are two different types of gesture, key board gesture and mouse gesture. Here is a class diagram of this.

InputGesture

Now lets define the new commands. We are going to make one class and define commands in it. Here is a code to define two commands.

  1: public class MyCommandClass
  2: {
  3:     private static RoutedCommand firstCommand;
  4:     private static RoutedCommand secondCommand;
  5: 
  6:     static MyCommandClass()
  7:     {
  8:         InputGestureCollection inputs1 = new InputGestureCollection();
  9:         inputs1.Add(new KeyGesture(Key.F, ModifierKeys.Alt, "Alt+F"));
 10:         firstCommand = new RoutedCommand("FirstCommand", typeof(MyCommandClass), inputs1);
 11: 
 12:         InputGestureCollection inputs2 = new InputGestureCollection();
 13:         inputs2.Add(new KeyGesture(Key.S, ModifierKeys.Alt, "Alt+S"));
 14:         secondCommand = new RoutedCommand("SecondCommand", typeof(MyCommandClass), inputs2);
 15:     }
 16: 
 17:     public static RoutedCommand FirstCommand
 18:     { 
 19:         get 
 20:         { 
 21:             return firstCommand; 
 22:         } 
 23:     }
 24: 
 25:     public static RoutedCommand SecondCommand
 26:     { 
 27:         get 
 28:         { 
 29:             return secondCommand; 
 30:         } 
 31:     }
 32: }
 33: 

Now lets take a look at XAML. We define the command binding in window level because we are going to define its handler in window class. And then set the commands with buttons. Here is a piece of code to define the command binding at window level.

  1: <Window.CommandBindings>
  2: 	<CommandBinding Command="local:MyCommandClass.FirstCommand" Executed="FirstCommandBinding_Executed"/>
  3: 	<CommandBinding Command="local:MyCommandClass.SecondCommand" Executed="SecondCommandBinding_Executed"/>
  4: </Window.CommandBindings>
  5: 

And here is a code to define commands with buttons.

  1: <Button Name="btnCopy" Margin="5" Grid.Column="0" Grid.Row="1"
  2: 	Content="First Command" Command="local:MyCommandClass.FirstCommand">
  3: 

Now lets take a look at complete program. Here is complete XAML code of the program.

  1: <Window x:Class="WpfCommand.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     xmlns:local="clr-namespace:WpfCommand"
  5:     Title="RoutedCommand" Height="300" Width="300">
  6:     <Window.CommandBindings>
  7:         <CommandBinding Command="local:MyCommandClass.FirstCommand" Executed="FirstCommandBinding_Executed"/>
  8:         <CommandBinding Command="local:MyCommandClass.SecondCommand" Executed="SecondCommandBinding_Executed"/>
  9:     </Window.CommandBindings>
 10:     <Grid Background="AliceBlue">
 11:         <Grid.RowDefinitions>
 12:             <RowDefinition/>
 13:             <RowDefinition/>
 14:         </Grid.RowDefinitions>
 15:         <Grid.ColumnDefinitions>
 16:             <ColumnDefinition/>
 17:             <ColumnDefinition/>
 18:         </Grid.ColumnDefinitions>
 19:         <TextBlock Grid.Row="0" Grid.ColumnSpan="2" 
 20:                    FontSize="20" FontWeight="Bold" Foreground="Navy" 
 21:                    VerticalAlignment="Center" HorizontalAlignment="Center">
 22:             RoutedCommand Example
 23:         </TextBlock>
 24:         <Button Name="btnCopy" Margin="5" Grid.Column="0" Grid.Row="1"
 25:                 Content="First Command" Command="local:MyCommandClass.FirstCommand">
 26:         </Button>
 27:         <Button Name="btnPaste" Margin="5" Grid.Column="1" Grid.Row="1"
 28:                 Content="Second Command" Command="local:MyCommandClass.SecondCommand"/>
 29:     </Grid>
 30: </Window>
 31: 

Here is complete C# code of the program.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Windows;
  6: using System.Windows.Controls;
  7: using System.Windows.Data;
  8: using System.Windows.Documents;
  9: using System.Windows.Input;
 10: using System.Windows.Media;
 11: using System.Windows.Media.Imaging;
 12: using System.Windows.Navigation;
 13: using System.Windows.Shapes;
 14: using System.ComponentModel;
 15: 
 16: namespace WpfCommand
 17: {
 18:     /// <summary>
 19:     /// Interaction logic for Window1.xaml
 20:     /// </summary>
 21:     public partial class Window1 : Window
 22:     {
 23:         public Window1()
 24:         {
 25:             InitializeComponent();
 26:         }
 27: 
 28:         private void FirstCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
 29:         {
 30:             MessageBox.Show("First Command", "RoutedCommand");
 31:         }
 32: 
 33:         private void SecondCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
 34:         {
 35:             MessageBox.Show("Second Command", "RoutedCommand");
 36:         }
 37:     }
 38: 
 39:     public class MyCommandClass
 40:     {
 41:         private static RoutedCommand firstCommand;
 42:         private static RoutedCommand secondCommand;
 43: 
 44:         static MyCommandClass()
 45:         {
 46:             InputGestureCollection inputs1 = new InputGestureCollection();
 47:             inputs1.Add(new KeyGesture(Key.F, ModifierKeys.Alt, "Alt+F"));
 48:             firstCommand = new RoutedCommand("FirstCommand", typeof(MyCommandClass), inputs1);
 49: 
 50:             InputGestureCollection inputs2 = new InputGestureCollection();
 51:             inputs2.Add(new KeyGesture(Key.S, ModifierKeys.Alt, "Alt+S"));
 52:             secondCommand = new RoutedCommand("SecondCommand", typeof(MyCommandClass), inputs2);
 53:         }
 54: 
 55:         public static RoutedCommand FirstCommand
 56:         { 
 57:             get 
 58:             { 
 59:                 return firstCommand; 
 60:             } 
 61:         }
 62: 
 63:         public static RoutedCommand SecondCommand
 64:         { 
 65:             get 
 66:             { 
 67:                 return secondCommand; 
 68:             } 
 69:         }
 70:     }
 71: }
 72: 

This is the output of the program.

RoutedCommandOutput

Now when we click on the first button or press Alt+F then there is a message box display with “First Command” and when we click on second button or press Alt+S then there is a message box display with “Second Command”.

Advertisements

Responses

  1. […] saw how to bind the build in command with tool bar here. We also saw how to create a routed command here. Now we are going to combine these two concepts and create a routed command and bind it with tool […]

  2. […] but with our own created routed command. We already created one example of creating routed command here. Here is our class of routed […]

  3. […] Creating Routed Commands […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: