28 September 2005

XAML provides two ways to set a property, either through a Property Attribute or through a Property Element. We covered Property Attributes last time, this time we will focus on the Property Element.

A Property Element is an element that, instead of creating an object instance, set the value of a property. The following is equivalent to the example given in Part I but uses Property Elements instead of Property Attributes.

<Button xmlns="http://schemas.microsoft.com/winfx/avalon/2005">
  <Button.Content>
    Hello, World!
  </Button.Content>
</Button>

The loader distinguishes a Object Element from a Property Element by the presents of the '.' in the name. The first part of the name is a reference to a type and the second part, after the dot, refers to the property name (we will discuss exactly why this is when we discuss Attached Properties). Here we use Button in both Property Elements. Property Elements are necessary when the value of the property cannot be expressed as a string. For example, you can use a Property Element to set the content of a Button to be another Button (not sure why you would want that, but suspend disbelieve for a second) as in,

<Button xmlns="http://schemas.microsoft.com/winfx/avalon/2005">
  <Button.Content>
    <Button Content="Hello, World!" />
  </Button.Content>
</Button>

This can be written more explicitly as,

<Button xmlns="http://schemas.microsoft.com/winfx/avalon/2005">
  <Button.Content>
    <Button>
      <Button.Content>
        Hello, World!
      </Button.Content>
    </Button>
  </Button.Content>
</Button>

As you can see, using Property Elements can become a bit repetitive. To mitigate this, one of the properties of a class can be designated as the Content Property which tells the load to assign direct content of the element to that property. In the case of Button (and all ContentControls) the Content Property is, oddly enough, set to the property named Content. This allows us to write something like,

<Button xmlns="http://schemas.microsoft.com/winfx/avalon/2005">
  Hello, World!
</Button>

for the first example above, and,

<Button xmlns="http://schemas.microsoft.com/winfx/avalon/2005">
  <Button>Hello, World!</Button>
</Button>

for the second. Most Avalon classes have a Content Property defined. For Panel's, such as DockPanel, Grid, and Canvas, their Content Property is set to their Children collection. Any direct content of a Panel is assumed to be content intended for the Children property. This means the following,

<StackPanel xmlns="http://schemas.microsoft.com/winfx/avalon/2005">
  <Button>OK</Button>
  <Button>Cancel</Button>
</StackPanel>

is an abbreviated version of,

<StackPanel xmlns="http://schemas.microsoft.com/winfx/avalon/2005">
  <StackPanel.Children>
    <Button>
      <Button.Content>
        OK
      </Button.Content>
    </Button>
  <Button>
    <Button.Content>
      Cancel
    </Button.Content>
  </Button>
  </StackPanel.Children>
</StackPanel>

Now, with Property Elements and Content Property Attributes and Object Element and Property Attribute from Part I, we have covered the fundamentals of XAML.



blog comments powered by Disqus