Building WPF Applications
451
Data templates
— Data templates let you bind data to control properties in ways that are not
possible in Windows Forms controls. For example, you can make a listbox display several
pieces of text and a picture for each of the items it contains.
Scalable controls
— A program can scale WPF controls to any degree without distorting them.
For example, you can zoom in on a curve or a string as much as you want and the result will
still appear smooth, not pixelated as it would if you zoomed in on a bitmapped image.
Although WPF has many advantages, it also has some disadvantages, the biggest of which is its com-
plexity. To really get the most out of it, you need to learn about its controls, pens, brushes, styles, tem-
plates, data templates, flow documents, resources, property triggers, event triggers, animations, themes,
printing, data binding, commanding, transformations, page navigation, and many other topics. WPF
has some amazing capabilities but taking advantage of them can be challenging.
The good news is that building simple programs is relatively easy. Most of what you’ve already
learned in this book still applies, and as the following section explains, you can use it fairly easily
to build basic WPF applications.
BUILDING WPF APPLICATIONS
Building a simple WPF application in Visual Studio is a lot like building a Windows Forms application.
To start a new WPF application, open the File menu, expand the New submenu, and select Project.
On the New Project dialog, expand the Visual C# category, open the Windows subcategory, and select
WPF Application. Enter a name for the new project, select a directory to hold it, and click OK.
So far this is almost exactly like building a Windows Forms application. When the new project
appears, however, the main object you see is a
Window not a Form.
Figure 40-1 shows Visual Studio displaying a new project. Much of this should look familiar. The
Toolbox is still on the left, the Solution Explorer is on the upper right, and the Properties window is
on the lower right.
The main editing area displays tabs much as a Windows Forms project does. The tab labeled
MainWindow.xaml.cs is a C# code window much like those that contain event handlers for a
Form.
The designer window shown in Figure 40-1 allows you to edit the WPF controls on the
Window.
You can use the Toolbox to place WPF controls on the
Window much as you place Windows Forms
controls on a
Form. Select a control (or the Window) and then use the Properties window to set the
selected object’s properties.
To make an event handler for a control, select the control in the designer. Then click the Events
button on the Properties window and double-click the event just as you would in a Windows Forms
application. Visual Studio creates an empty event handler in the MainWindow.xaml.cs file and
opens it in the code editor.
The event handlers and other code that sits behind the user interface is called
code-behind.
596906c40.indd 451 4/7/10 12:35:18 PM