Controls and Content

Earlier in this chapter, a control was defined as an element that is intended for user input. All XAML elements can respond to mouse and keyboard events, but elements such as the Grid do not, by default, do anything with those events. Controls possess functionality to process events and respond to them in some way.

The base Control class implements a lot of functionality needed by all controls, including:

  • Font properties for sizing and formatting of text
  • Properties for a background, foreground, and border
  • The TabIndex property to control the tab order
  • The capability to use a template to radically alter the appearance of a control

Most developers will have no issues in using these properties, so detailed descriptions are not necessary. This chapter does include a section on Brushes, which is relevant for setting Foreground and Background properties. Also, a section on control templates is included toward the end of the chapter to describe how to alter the fundamental appearance of a control.

Content Controls

Some controls in XAML descend from a base class named ContentControl, and have a property named Content. It is of type Object. You can set the Content property to any .NET object, and XAML will attempt to render it.

The Content property can be set explicitly, or by placing something between the tags of a ContentControl. Both of these are shown in this XAML example:

<Button Content="Save" />
<Button>
    Cancel
</Button>

These two ways of expressing a Button ...

Get Professional Visual Basic 2012 and .NET 4.5 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.