Commands in WPF


Commands are used to share grouped actions within an application in different ways. Sometimes we need to perform the same activity, WPF provides us a feature called Command to make our work easier and faster.
 
 There are basically four type of Commands:
 
 Application Commands:
It include File Level Commands. For Example File:-> Open
 Edit Commands: It provides editing capabilities.
 Component Commands: It provides Scroll-up and Scroll-Down type of Commands. It is also useful to move to the front of the section of text and move to the End.
 Media Commands: It provides Multimedia type of commands like Play, Pause...
 
 Note: Most of the Commands, which are used by different controls are available in the Application Commands.
 
 Now we take an example of a Command. In this example the user will be able to invoke the custom command by pressing the Ctrl+Shift+M . Here we define a Command:

 
 public
static RoutedCommand myCommand;
         public Window1()
         {
             InputGestureCollection myfirstInput =new InputGestureCollection();
             myfirstInput.Add(new KeyGesture(Key.M, ModifierKeys.Control | ModifierKeys.Shift));
             myCommand = new RoutedCommand("Go", typeof(Window1), myfirstInput);
         }
 
 In this Example, first we declare a static RoutedCommand:
 
 
public static RoutedCommand myCommand;
 
 After that we create an InputGestureCollection:
 
 
InputGestureCollection myfirstInput =new InputGestureCollection();
 

Note: An Input Gesture can be associated with a command so that when the gesture is performed the command is invoked. There are two Input Gestures that are supported: MouseGesture and KeyGesture. These represent the potential activating invocations from a keyboard shortcut or a drag-drop-drawing event. Once we add the desired input gestures to the InputGestureCollection, our command can be accessed through any of the key combinations we entered.
 
 After that we add the Input Gesture Items:
 
 myfirstInput.Add(new KeyGesture(Key.M, ModifierKeys.Control | ModifierKeys.Shift));
 
 After that we create the routed command with the name, type, and the InputGestureCollection variable which we have created.
 
 myCommand = new RoutedCommand("Go", typeof(Window1), myfirstInput);

Up Next
    Ebook Download
    View all
    Learn
    View all