CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Beginning Windows Presentation Foundation Project

By Dotnet Tutor
Posted On Jun 09,2009
Article Rating:
Be first to rate
this article.
No of Comments: 2
Category: WPF
Print this article.

Beginning Windows Presentation Foundation Project

 

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

/Articles/ArticleFiles/IMG/253/Wpf01.png

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.

/Articles/ArticleFiles/IMG/253/Wpf02.png

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.

/Articles/ArticleFiles/IMG/253/Wpf03.png

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.

/Articles/ArticleFiles/IMG/253/Wpf04.png

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.

/Articles/ArticleFiles/IMG/253/Wpf05.png

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.

/Articles/ArticleFiles/IMG/253/Wpf06.png

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.

 

/Articles/ArticleFiles/IMG/253/Wpf07.png

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

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.

/Articles/ArticleFiles/IMG/253/Wpf08.png

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

/Articles/ArticleFiles/IMG/253/Wpf09.png

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.

/Articles/ArticleFiles/IMG/253/Wpf10.png

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

/Articles/ArticleFiles/IMG/253/Wpf11.png

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.

/Articles/ArticleFiles/IMG/253/Wpf12.png

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.

/Articles/ArticleFiles/IMG/253/Wpf13.png

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.

/Articles/ArticleFiles/IMG/253/Wpf14.png

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.

/Articles/ArticleFiles/IMG/253/Wpf15.png

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

This article is based on the book Programming Windows Workflow Foundation: Practical WF Techniques and Examples using XAML and C#

 

Similar Articles