SIGN UP MEMBER LOGIN:    
ARTICLE

Commands in WPF

Posted by Mahak Gupta Articles | WPF with C# March 01, 2012
Commands are used to share the grouped actions within an application in different ways.
Reader Level:

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);

Login to add your contents and source code to this article
share this article :
post comment
 

thnx for sharing ....

Posted by Sanjoli Gupta Mar 01, 2012

Its a very nice and useful article.

Posted by Adora Krause Mar 01, 2012

It's a good start towards WPF Keep it up and thanks for sharing....

Posted by Amit Maheshwari Mar 01, 2012

Thanks for sharing this article.

Posted by Jimmy Underwood Mar 01, 2012
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor