What Are Controls?
Controls are the building blocks of an application's user
interface. They are interactive features such as text boxes, buttons, or
listboxes. You may be familiar with similar constructs from other
user-interface technologiesmost UI frameworks offer an abstraction similar to a
control. However, WPF is somewhat unusual, in that controls are typically not
directly responsible for their own appearance. Many GUI frameworks require you
to write a custom control when customizing a control's appearance. In WPF, this
is not necessary: nested content, and templates offer powerful yet simpler
solutions. You only need to write a custom control if you need behavior that is
different from any of the built-in controls.
 |
Many WPF user-interface elements are not controls.
For example, shapes like Rectangle and Ellipse
have no intrinsic behaviorthey are just about appearance.
|
|
Figure 3-1 shows
how a control fits into a program. As you can see, the visible parts of the
control are provided by its template, rather than the control itself. The
control is not completely disconnected from these visuals, of course. It uses
them to present information to the user. Moreover, since the visuals are all
that the user can see, they will be the immediate target of any user input.
This means that although visuals can be replaced, the replacement has certain
responsibilitiesthere is a form of contract between the control and its
visuals. The use of templates to replace visuals is discussed in
Chapter 5.
 |
You may be familiar with the Model View Controller
(MVC) concept. This is a way of structuring the
design of interactive systems. MVC has been interpreted in many different ways
over the years, but broadly speaking, it always breaks the design down into
objects representing the underlying data (the Model), objects that display that
data (the View), and objects that manage input from the user and interactions
between the model and view (the Controller).
MVC is a concept that can be used at many different scales, and
it is somewhat unusual to apply it at the level of an individual control.
However, if you are accustomed to the MVC way of looking at things, you may
find it helpful to think of data binding as a way of attaching a Model, the
visuals as the View, and the control as the Controller.
|
|
While the control makes itself visible to the user through its
visuals, it makes its services available to developers mainly through an API,
shown on the left of Figure 3-1.
Controls offer commands to represent
supported operations. For example, a text box might offer Cut, Copy, and Paste
commands. Controls offer properties to
provide a means of modifying either behavior or appearance. Controls raise events
when something important happens, such as receiving some form of input.
Finally, some functionality may be exposed through methods
. Commands, properties, and events are preferred because they are easier to use
from markup, and will be supported by design tools. However, for features that
would only ever be used from code, methods may be a more appropriate form of
API.
Of course developers and designers are not the only people who
will use controls. Controls also need to respond to users, so let's look at how
input is handled in WPF.
 |