.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 Tutorial : Part IV

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

Commands in WPF Tutorial: Part IV In this Tutorial we will explore on Command Objects and Command Types.

Command Objects

In Part III of this article series; we have seen how Input Binding is helpful. In this article we will explore on Command Objects and Command Types.

You can also refer my other articles of the same series Part I, Part II, Part III

A Command Object identifies a particular Command. It doesn't know how to handle the Command; as we have seen that is the job of Command Binding.

Command Objects are typically made available through static properties. Such as ApplicationCommands.Properties

There are several places from which you can get hold of a Command Object.

Some Controls define commands. For Example: the Scrollbar Control defines one for each of its actions, and makes these available in static fields.

Such as LineUpCommand, PageDownCommand.

However, most commands are not unique to a particular control.

Some are related to application level actions such as: "New File" or "Open".

WPF provides a set of classes that define standard commands. Such as follows:


Although the standard commands cover a lot of the common features found in many applications, applications usually have functionality of their own not addressed by the standard commands. We can use the command system for application-specific actions by defining custom commands.

Application Commands

Earlier we have seen how to use Copy, Cut and Paste Commands in WPF. The Clipboard Commands are part of ApplicationCommands.

We will explore more on ApplicationCommands Commands, such as: Open.

So let's have two Button saying caption as Open and a TextBox to display the file content.


As you see above we have the Button and TextBox placed.

Now let's add the Command.


Code view of the above image

As you see in the above XAML display we have two events such as Executed and CanExecute. These events are Command specific events to be handled to execute the command.

So lets handle the command.


Code view of the above picture
        private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
            e.CanExecute = true;

We have to handle the CanExecute to True otherwise the Command would not be handled.


Code view of the above image
        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Filter = "Text files (*.txt)|*.txt";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                txtData.Text = "To Do: \n\nNormal File Open Operation";

Now in the Executed event handler we can write our code to execute.

As you see in above code display, I have opened a file dialog to open the Text Files.

Now let's run the application.

As soon as you click on the Open button the Command would get executed and we would have the Open File Dialog Box.


After opening the file:


So we have successfully used the Open Command.

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

Hope this article helps.

 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