.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Commands in WPF: Part II

Posted By:Diptimaya Patra       Posted Date: May 16, 2010    Points: 25    Category: WPF    URL: http://www.dotnetspark.com  

This article is a part of series of my article on commands in WPF and how to used it in our application


In Part I of this article series we have seen the basics of using Commands. In this article we will explore more on that.

Concepts of Command System

There are five concepts related to Command System:
  1. Command Object

    - An object identifying a particular Command, such as Copy or Paste.
  2. Input Binding

    - An association between a particular input (e.g. CTRL+C) and a Command (e.g. Copy)
  3. Command Source

    - The object that invoked the Command, such as a Button or Input Binding.
  4. Command Target

    - The UI element that will be asked to execute the Command.
  5. Command Binding

    - A declaration that a particular UI element knows how to handle a particular Command.
Let's modify our Application a bit. As follows:



Code view of the above image

As you see above we have a Button that will display the properties when it is clicked.

Remember that in Windows ALT+ENTER is the Command that will execute the Command for displaying Properties.

Let's have implemented the concepts that we have just discussed about.


Code view of the above image
        public Window1()
            CommandBinding cb = new CommandBinding(ApplicationCommands.Properties);
            cb.Executed += new ExecutedRoutedEventHandler(cb_Executed);
        void cb_Executed(object sender, ExecutedRoutedEventArgs e)
            MessageBox.Show("Application: Commands\n\nType: WPF Application", "Properties");

As you see from the above Code Display, we have a CommandBinding object that takes the argument as a Command.

On Executed event handler we have just displayed a MessageBox.

Let's run the application.


When we click on Properties Button the following Message Box is displayed.


So we can have our own display if we want to display our custom Properties Window in this event handler.

You can also download the sample project used in the above example.

Hope this article helps learning the CommandBinding concept.
 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend