BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

Creating a WPF Project in Visual Studio

The third part of the Windows Presentation Foundation Fundamentals tutorial explains how a new WPF Application project can be created in Visual Studio and describes the basic Visual Studio windows used to edit the XAML design and code.

Visual Studio

Visual Studio is an ideal choice of integrated development environment if you are creating WPF applications. It allows you to edit the XAML for your application directly with a text editor that assists you as you type. This completes keywords automatically as you type, speeding up the process and reducing the risk of misspellings. It also highlights errors in your XAML code and warns you of other problems.

When you don't want or need to edit XAML directly, Visual studio provides designer windows that allow you to construct your application by dragging and dropping controls from a toolbox, then modifying the controls by manipulating them with the mouse. Further changes can be applied by browsing a list of the available properties for a selected control and editing their values with rich property editors.

Once you have created your XAML and the code of your application, Visual Studio will compile it into a finished product. During development you can run your partially complete application in the Visual Studio debugger, which provides assistance in identifying problems.

In this article we'll look at the basics of creating a new WPF application project using Visual Studio. We'll see only the few elements that are required to develop WPF software. This will be an overview aimed at those with some Visual Studio experience, perhaps those who have developed Windows Forms software, console applications or code libraries. The screenshots were created using Visual Studio 2012, though the processes described are similar in other versions.

Creating a WPF Project

To create a new WPF project you need to start Visual Studio and choose the "New Project..." option from the start screen. If you have disabled the start screen, open the File menu and choose New, then Project. The New Project dialog box should be displayed.

Visual Studio WPF Templates

The New Project dialog box shows a categorised set of solution templates that can be used to create many different types of project. The WPF templates are listed in the Windows section, which can be selected in the tree at the left. In the image, the Windows section within the C# branch of the tree is shown. The actual organisation of this tree is dependent upon your Visual Studio configuration. The above image is from an installation that is set up for C# development. However, other language options are still available by expanding the Other Languages section.

There are several WPF project templates available. They determine the type of assembly that will be generated upon compilation and the contents of the project when it is first created. The four most common options are:

  • WPF Application. This project template is used to create interactive Windows applications containing windows and dialog boxes. Most of the examples in the WPF tutorial use this project template.
  • WPF Browser Application. Using this template you can create WPF applications that run within a web browser. These applications can be easier to distribute but have many limitations. For example, they do not have full access to operating system functions and the file system. They are not suited for general use on the World Wide Web, as they are only supported by a limited number of browsers and the visitor must have the correct version of the .NET framework installed. The compiled code is known as a XAML Browser Application (XBAP).
  • WPF User Control Library. As with Windows Forms, you can create your own reusable controls. A WPF User Control library is used to create such "user controls" and compiles into a dynamic linked library (DLL) that can be referenced by other projects. User controls are generally created in XAML by combining native controls and other user controls and perhaps adding some event code and properties. They can be created quickly within minimal coding.
  • WPF Custom Control Library. This template is used to create reusable custom controls. As with user control libraries, the output is a DLL containing the new controls. The DLL can be referenced by other projects. The key difference between the two types of control is that custom controls tend to be created in code with little or no XAML. They are more flexible but take more time to create.

For our quick tour of the Visual Studio windows select the "WPF Application" template. Add a name for the project in the Name TextBox and click "OK" to create the new solution.

10 February 2013