SIGN UP MEMBER LOGIN:    
ARTICLE

Commands in WPF - Part IV

Posted by Diptimaya Patra Articles | WPF with C# April 05, 2010
Tags: Commands, WPF
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.
Reader Level:
Download Files:
 

Command Objects

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:

image1.gif

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.

image2.gif

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

Now let's add the Command.

image3.gif

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.

image4.gif

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

image5.gif

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.

image6.gif

After opening the file:

image7.gif

So we have successfully used the Open Command.

Hope this article helps.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    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!
Nevron Gauge for SharePoint
Become a Sponsor