This chapter is from the book

This chapter is from the book

A XAML file, like all XML files, must have a single root object element. Therefore, it should come as no surprise that object elements can support children that are more than just the property elements introduced in the preceding chapter. Property elements aren’t true children as far as XAML is concerned.

This chapter examines the types of children that object elements can have. It also summarizes all the keywords in the XAML language namespace, although many of them are discussed in depth in the two chapters that follow.

Children of Object Elements

An object element can have three types of children:

A value for a content property

Collection items

A value that can be type-converted to the object element

We’ll look at the first two types of children now, but save the third type for Chapter 4, “Extending XAML.”

The Content Property

Most classes designate a property (via a custom attribute) that should be set to whatever content is inside the XML element. This property is called the content property, and it is really just a convenient shortcut to make the XAML representation more compact.

Button’s Content property is (appropriately) given this special designation, so the following Button:

FIGURE 2.1 A simple ListBox has three children in its Items collection.

Collection Items

XAML enables you to add items to the two main types of collections that support indexing: lists and dictionaries.

Lists

A list is any collection that implements System.Collections.IList, such as System.Collections.ArrayList or numerous collection classes defined by various frameworks. For example, the following XAML adds two items to a ListBox control whose Items property is an ItemCollection that implements IList:

In all these cases, the code works because ListBox’s Items property is automatically initialized to any empty collection object. If a collection property is initially null instead (and is read/write, unlike ListBox’s read-only Items property), you need to wrap the items in an explicit element that instantiates the collection. ListBox does not act this way, so an imaginary OtherListBox element demonstrates what this could look like:

Dictionaries

ResourceDictionary is a commonly used collection type in all XAML-based frameworks that you’ll see more of in Chapter 16, “Resources.” It implements System.Collections.IDictionary, so it supports adding, removing, and enumerating key/value pairs in procedural code, as you would do with a typical hash table. In XAML, you can add key/value pairs to any collection that implements IDictionary. For example, the following XAML adds two Colors to a ResourceDictionary:

This leverages the XAML Key keyword (defined in the secondary XML namespace), which is processed specially and enables us to attach a key to each Color value. (The Color type does not define a Key property.) Therefore, the XAML is equivalent to the following code:

In addition to keywords in the XAML language namespace, XAML also supports two special attributes defined for XML by the World Wide Web Consortium (W3C): xml:space for controlling whitespace parsing and xml:lang for declaring the document’s language and culture. The xml prefix is implicitly mapped to the standard XML namespace; see http://www.w3.org/XML/1998/namespace.