Your message has been sent.
This article has been saved to your account.
Go to my account
This article has been emailed to your Kindle.
Send this article
This article by Jayaram Krishnaswamy introduces the reader accustomed to working with the traditional graphic user interface in earlier versions of VB to Windows Presentation Foundation. Importantly, it introduces the reader to the XAML's declarative format and what it means in the design interface of VS 2008. WPF can do a great deal more than what is described in this article. The power of markup extensions such as declarative binding, dynamic resource, template binding and many others are not discussed. It is hoped that the reader will be up and running WPF projects based on his previous experience after reading this article.
WPF, an acronym for Windows Presentation Foundation is a subsystem of class libraries for WinFX and it enables the user to get a richer experience bringing together UI, Documents, media etc. A XAML (Extensible Application Markup Language) file which is at the heart of a WPF project can be created in several ways that includes the Notepad text editor, the Expression Blend which requires another download from Microsoft, but may not provide a easy to use XAML file to use in VS, and the Visual Studio editions except the express edition. XAML is presently specific to windows platform and is a XML formatting language and not an application programming interface. I will be mostly showing how to get some hands-on experience with a WPF project using the Visual Studio 2005 interface and the template files that you may access with the Windows SDK installed.
Creating a WPF Project
From File | New | Project click open the New Project window as shown in the next figure. Click on Visual Basic and expand its contents. Under .NET 3.0 FrameWork (It is assumed that you have installed NET 3.0 Framework) choose the Windows Application (WPF).
Now highlight the Windows Application (WPF) and change the name of the application to some name of your choice. For this article it is changed to AppWPF. Click on the OK button after typing a name of your choice. This creates the necessary file/folders for the application as shown in the next figure.
There are two XAML files created in the project. The App.xaml and the Windows1.xaml file. Delete the Windows1.xaml and add a new item as shown with the name BasicControls.xaml.
With this new item added you may need to change the App.xaml file as shown below.
The StartupUri has been changed from the original Windows1.xaml to BasicControls.xaml. With this change made you can now display the BasicControls.xaml file together with its design as shown in the next figure.
This represents a 300 X 300 window which can be used as a container for other controls. You also notice the reference to the namespaces that are required and the XML syntax with the attribute of the project for the window.
Placing Controls on the Window
Placing Controls automatically creates XAML code.
Placing controls on this window is as easy as dragging from the Tools and dropping on to this window. The next picture shows a button and a textbox dragged and dropped onto this window.
The necessary code for these controls gets automatically added as the controls are placed. After the two controls are added, the xaml file gets changed as shown. The Button and Textbox properties are the defaults which may be modified as will be seen later in the article.
Title="AppWPF" Height="300" Width="300"
<Button Height="23" Margin="94,0,123,39"
Margin="43, 126, 0,115"
Adding code automatically updates the window design.
Inserting a declarative code into the BasicControls.xaml file will automatically add the control defined by that code to the design window.
Add this code to the xaml file after
As soon as you type "<", the intellisense gets fired up and you will see a drop-down list of items that you can insert as shown in the next figure.
Now you click on the Textbox (or whatever else you wish to place). This adds
<Textbox Name="TextBox2" Height="20" Margin="89.5,96.5,0,0"
The property window for the TextBox2 shown can also be used to make changes. You can also move, or adjust the dimensions of the controls using the mouse. The various controls provide a very rich interface for the designer in manipulating the controls.
All 'Hello World' programs used a button click to demonstrate the workings of the code or how the events were handled. In this tutorial also you will demonstrate the click event along the same lines. In the Solution Explorer only a few items are seen but there are lot more files in the project. Click on the middle toolbar just above the project as shown in the next figure.
This will allow you to see all the files / folders in the project displayed (every folder expanded out) as shown.
This is vastly different from a legacy windows project. The references to the Presentation Foundation are all in the three references, PresentationCore, PresentationDesignDeveloper and PresentationDesignFramework.
In order to appreciate the rich designer support you have to go to the ,Object Browser and look at the references. For example just the PresentationCore has the following namespaces shown in the next figure.
The BasicControls.xaml file also has the code behind file, BasicControls.xaml.vb, as shown in the next figure.
In the code page, the drop-down control displaying BasicControls presently has all the objects on this window listed in its menu. You can find the Button as well. With the button chosen you can use the second drop-down to access all the events of the Button in the second drop-down (presently showing Declarations). In this manner the button click event was chosen from the second drop-down. Here the Button1_Click has been set to display "Click is registered" in Textbox1 when the button is clicked. You can find the reference to this in the Object Browser as shown in the next figure.
Object Browser is an extremely valuable resource that you should seek out to understand the underlying logic, the arguments of a function call, etc.
When you build and execute the program and click on the button this is what you will see displayed. The top part is the design window and the bottom is the window when clicked.
At this point you might be wondering how to improve the look and feel. Indeed the form looks drab since none of the properties have been used except for the most basic. The next figure shows how you may change the appearance by inserting the property attributes directly into the XMAL file. You will be better off using the intellisense rather than trying to guess the property based on your previous 'Windows' experience as shown in the next figure. You may also add attributes from the property window of the object which you can view when the object is highlighted (or clicked) in the design pane.
The variety of attributes is just too many and when in doubt you will be able to drill down to the one you want to use in the Object Browser.
The next code listing shows a few more attributes added to the Textbox1. As you might have seen in the intellisense pop-up windows, there is a large number of properties that you can tweak and events that you can trigger. Notice the [.] notation for the TextElement in the code listing, FontFamily being the child of the parent TextElement.
ToolTip="Xaml TextBox" FontWeight="Bold"
When the program is executed you will see the following displayed.
The article describes the steps to create a WPF project. The Design <-->Declarative Code interactivity is also described. The placing of controls and adding event handling code to the code behind page is explained with an example. While testing the "AutoWordSelection" did not function as it should by its definition. You may look up this in the 'Help'.
eBook Price: $26.99
Book Price: $44.99
About the Author :
Dr. Jay Krishnaswamy is a graduate of the Indian Institute of Science, Bangalore writes on database and web development related topics to several computer programming related web sites. He is an active participant in several forums and discussion groups. Before working in the IT industry as a Microsoft Certified Trainer and a Siebel Certified consultant he taught at several institutes of technology and universities in India, Australia, Brazil and the USA. Links to his articles may be found at his web site or the blog. He lives in Plainsboro, NJ, USA and may be reached at email@example.com