Illustrated WPF

Über dieses Buch

Windows Presentation Foundation is Microsoft's API for creating Windows applications. It gives the programmer the ability to produce dazzling, graphics–rich programs easily without having to delve into the messy details of the graphics subsystem.

To use this power, however, the programmer must learn new concepts for laying out pages and displaying graphics. Illustrated WPF presents these concepts clearly and visually—making them easier to understand and retain.

WPF programming is a large topic with lots of areas to cover. Most of the chapters in this book address one or two topics and cover them in detail. This chapter and the next, however, are different. In these chapters, I’ll cover a number of topics that will lay the groundwork for the rest of the text.

The previous chapter was a gentle introduction to the WPF programming framework and the mechanics of building a program using just C#. In this chapter, I’ll cover more of the architecture of WPF and introduce the XAML markup language.

One of the most important concepts of WPF is that the user interface is a tree of WPF class objects. That tree has a single node at the top, and each node in the tree can have zero or more child nodes.

Before WPF, user interfaces generally consisted of windows with statically placed controls. The programmer set the height and width of a control and placed it at a certain coordinate position on the window or panel, where it stayed. (To be fair, you could also dock controls to edges.)

WPF is a UI framework that excels in displaying visual content. In previous frameworks, different UI controls were specialized for particular purposes, and their visual presentations were more or less fixed. For example, a Label in the Windows Forms framework was a piece of static text. A Button was usually labeled with text but could also paint its background with an image.

You might have noticed by now that WPF uses properties extensively. The WPF classes have been specifically designed to use properties rather than methods, when possible, because properties are declarative, making them ideal for XAML’s declarative syntax. But beyond just the preference for properties, WPF has produced a whole new way of using properties and a set of services to support that method.

Oftentimes a visual element displays the representation of some underlying data value. In this case, you usually want to ensure that the visual element stays up-to-date and changes when the underlying data value changes.

Windows programming is event driven, which means that while a program is running, it can be interrupted at any time by user actions or system actions such as button clicks, key presses, or system timers. When this happens, the program needs to handle the event and then continue on its course. If you want your program to perform some set of tasks when a particular event occurs, you must write a method, called an event handler, to be called when the event occurs.

In Chapter 6, you learned about many of the most important WPF controls that present content. In this chapter, you’ll learn about other controls and elements you’ll need to have a rich and smoothly functioning application. You’ll start with the TextBox control and continue to menus, toolbars, and miscellaneous other elements.

In the previous chapter, you saw how to declare and save the value of a dependency property as a logical resource in a resource collection. Once the resource is declared, you can then use it anywhere below the declaration in the element tree, without having to re-define it each time. Also, if you need to change the value of the property, you can do it in just one place, and the change is reflected in all the locations that use it.

Most normal Windows programs start by showing the user a single main window. The user can then click buttons on the window or select menu items, and the program opens additional windows, which present the user with additional options or data.

In Chapter 8 you saw how to bind a control to a collection of data objects. In Chapter 13 you saw how to replace the default appearance of a control with an appearance of your own design. In this chapter, I’ll combine and extend these two concepts and show you how to specify the appearance of data you’ve mapped to a control.

In this chapter, I’ll cover two important controls that weren’t covered in the previous chapters: TreeView and TabControl. I’ll also cover the three new controls included in WPF 4.0, which is scheduled for release shortly after the release of this text.

WPF allows programmers to design programs with amazing graphic content with unprecedented ease. Text, however, still plays an important part in most programs and can even be the dominant component of some. Programs can use different amounts of text and use it in very different ways. In this chapter, you’ll learn some of the ways WPF provides for presenting text.

As you’ve seen throughout this text, WPF is a much more graphics-oriented framework than any previous Windows development framework. So far, I’ve covered graphics piecemeal, in the context of whatever topic I was covering at the time. In this chapter, I’ll cover graphics from a higher perspective and show how various graphics features apply across WPF as a whole.

A graphic animation, such as an animated cartoon, is a sequence of frames, where the objects in each frame are moved just a small bit from where they were in the previous frame. When the frames are shown in rapid succession, the objects in the frames appear to be moving.

In most of the areas I’ve covered throughout this text, WPF allows the programmer far more expressiveness than in previous frameworks. The audio features it provides, however, aren’t particularly impressive. Instead, they are little more than wrappers around Win32 APIs or the Windows Media Player.