Windows Presentation Foundation Project - Basics of Working

Exclusive offer: get 50% off this eBook here
Programming Windows Workflow Foundation: Practical WF Techniques and Examples using XAML and C#

Programming Windows Workflow Foundation: Practical WF Techniques and Examples using XAML and C# — Save 50%

A C# developer's book and eBook guide to the features and programming interfaces of Windows Workflow Foundation

$26.99    $13.50
by Dr.Jay Krishnaswamy | February 2008 | .NET Microsoft

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.

Introduction

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).

Windows Presentation Foundation Project - Basics of Working

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.

Windows Presentation Foundation Project - Basics of Working

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.

Windows Presentation Foundation Project - Basics of Working

With this new item added you may need to change the App.xaml file as shown below.

<Application x:Class="App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="BasicControls.xaml">
<Application.Resources>

</Application.Resources>
</Application>

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.

Windows Presentation Foundation Project - Basics of Working

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.

Windows Presentation Foundation Project - Basics of Working

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.

<Window x:Class="BasicControls"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AppWPF" Height="300" Width="300"
>
<Grid>
<Button Height="23" Margin="94,0,123,39"
Name="Button1"
VerticalAlignment="Bottom">Button</Button>
<TextBox HorizontalAlignment="Left"
Margin="43, 126, 0,115"
Name="TextBox1" Width="100"></TextBox>
</Grid>
</Window>

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 shown in the next paragraph.

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.

Windows Presentation Foundation Project - Basics of Working

Now you click on the Textbox (or whatever else you wish to place). This adds to the xaml file. Now to the opening tag of the textbox, you add a name attribute and call it TextBox2. Intellisense is also used in adding attributes as you will get a context sensitive listing of attributes for the chosen control. Also add other attributes such as width, height, alignment etc. With the code added as shown in the next paragraph you will see that the design pane has a new textbox as shown in the next figure.

 

Windows Presentation Foundation Project - Basics of Working

<Textbox Name="TextBox2" Height="20" Margin="89.5,96.5,0,0" 
VerticalAlignment="Top" HorizontalAlignment="Left"
Width="50"></TextBox*gt;

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.

Event Handling

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.

Windows Presentation Foundation Project - Basics of Working

This will allow you to see all the files / folders in the project displayed (every folder expanded out) as shown.

Windows Presentation Foundation Project - Basics of Working

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.

Windows Presentation Foundation Project - Basics of Working

The BasicControls.xaml file also has the code behind file, BasicControls.xaml.vb, as shown in the next figure.

Windows Presentation Foundation Project - Basics of Working

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.

Windows Presentation Foundation Project - Basics of Working

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.

Windows Presentation Foundation Project - Basics of Working

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.

Windows Presentation Foundation Project - Basics of Working

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.

Listing 1

<TextBox  HorizontalAlignment="Left" 
Margin="43,126,0,115"
Name="TextBox1" Width="150"
TextElement.FontFamily="Times Roman"
ToolTip="Xaml TextBox" FontWeight="Bold"
AutoWordSelection="True"
Foreground="BlueViolet"
Background="Aquamarine"
TextDecorations="Underline"
>
</TextBox>

When the program is executed you will see the following displayed.

Windows Presentation Foundation Project - Basics of Working

Summary

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'.

Programming Windows Workflow Foundation: Practical WF Techniques and Examples using XAML and C# A C# developer's book and eBook guide to the features and programming interfaces of Windows Workflow Foundation
Published: December 2006
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

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 jkrishnaswamy@comcast.net

Books From Packt

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software