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

Top 5 Contributors of the Month
Gaurav Pal

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

 Subscribe to Articles

Commands in WPF: Part III

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

This is the 3rd article of the series Commands in WPF. In this we will see how Input Binding is helpful using command in WPF application.


In Part 1 and Part II of this article series we have seen the concepts of Commands and we experienced Command Binding feature. In this article we will see how Input Binding is helpful.

As we discussed in our last article InputBinding is an association between a particular input and command.

We will modify our Application such that without Button Click how we can call the Properties Command.

So our UI will contain only the TextBox control, we would get rid of the Button that we used in our previous sample.



Code view of the above image

As you see above we have only a TextBox control that doesn't have any Commands associated.

Now in Code behind retain the CommandBinding. We have to just create an InputBinding object with the proper Command and KeyGestures.

Follow the below code:


Code view of the above image
        public Window1()
            InputBinding ib = new InputBinding(ApplicationCommands.Properties,
                                new KeyGesture(Key.Enter, ModifierKeys.Control));

            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 above we have created the InputBinding object with ALT+ENTER as the KeyGesture.

Now let's run the application and see by pressing the above key combination what happens.


And we have the Command successfully running on KeyGesture.

Now we can have key combination of our choice and it will handle the Properties Command.

Hope this article helps, understanding InputBinding.

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

 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