PF's new control set includes both familiar controls, such as the Label and TextBox that do more or less the same thing as their Windows Forms counterparts as well as new and unfamiliar controls, such as WrapPanel and Expander, which provide useful new features.
This article provides a brief overview of the most useful WPF controls, in alphabetical order. It explains broadly the purpose of each and, for the more complicated controls, gives a few hints about using them.
Before delving into the controls themselves, it's worth discussing two WPF features that give WPF controls abilities that Windows Forms controls lack: support for DirectX, and a more general content model.
As discussed in the first article in this series, one of WPF's goals is to take advantage of modern graphics hardware. WPF achieves this by using DirectX to build its controls, giving them such benefits as faster rendering, better video support, and transformations. Transformations give you the power to move, rotate, and stretch controls easily.
For example, the UsesDirectX sample program shown in Figure 1 (available in the downloadable code in both C# and Visual Basic versions) displays a group of controls that have been rotated by 20 degrees. The colorful control in the middle is a MediaElement control displaying a video of a Julia Set fractal.
|Figure 1. Terrific Transformations: Because they are built with DirectX, WPF controls support transformations such as translation, scaling, and rotation.|
The second basic change (which is less cool but probably more important than rotated video controls) is that WPF controls support a more complex content model than Windows Forms controls. In Windows Forms, many controls can hold a single specific piece of information. For example, the Windows Forms Label and Button controls have a Text
property that determines the text the controls display. A Button control can also display an image—but that's about it. You can't, for instance, place a TreeView control on a Button or put a video file in a Label.
But, regardless of the wisdom of doing so, such operations are quite possible in WPF. That's because instead of a single Text or similar property that determines control contents, most WPF controls have a Content property that lets them hold just about anything. A Button can contain simple text, a Label, a TextBox, a MediaElement playing a video, or even a TreeView. WPF automatically handles the events generated by whatever is inside the Button, so clicking on the contents makes the Button raise its Click event just as you would expect.
|Figure 2. Captivating Controls: Program ControlSamples demonstrates most of the key WPF controls.|
Many WPF controls can hold multiple child controls but even those that can hold only a single child are not particularly restricted, because that single child can be a container control that holds multiple children.
For example, the rotated group of controls shown in Figure 1 is actually one large Button containing a Grid that holds a set of Label, Image, and MediaElement controls. If you click on any of them, the Button raises its Click event (which restarts the video in the demo).
As you read through the following sections describing specific control types, you'll learn which ones are designed to hold a single child (Label, Button) and which can contain and arrange many children (Grid, StackPanel). After you get the hang of WPF's new content model, you can compose controls to build all sorts of interesting and powerful user interfaces.
With that background out of the way, the following sections describe the most useful WPF controls. The ControlSamples program (also in the downloadable code) shown in Figure 2 demonstrates the controls discussed here.
|Author's Note: The ControlSamples application is intended purely for demonstration purposes; I don't generally recommend putting every possible control on a single window.