WPF Part 4 - A closer look at XAML

This is only a short article, because there aren't so many things left at XAML I could talk about, but I think, it would be better to separate them from the last part of this series.

Ok, what I'm talking about? I will write about the built-in markup and language extensions and about the dependency and attached properties of XAML and WPF.

In the process of developing XAML you have many situations were you use an extension. For example, when you want to bind a property to maybe another property of an element. In this case you will use the binding extensions, which is a WPF markup extension.
In the Attribute-Syntax, markup extensions are included in braces ({...}):

  1. <TextBox Name="textBox1" />
  2. <TextBox Text="{Binding ElementName=textBox1, Path=Text}" />

In the Property-Element-Syntax the extension is defined like a normal element, as you can see here:

  1. <TextBox>
  2.   <Binding ElementName="textBox1" Path="Text" />
  3. </TextBox>

Markup extensions:

There are different types of markup extensions. Some are defined in the WPF and some in XAML. The following table shows you some WPF markup extensions:

Extension Description
Binding Defines a data binding between the value of an attribute.
DynamicResource The value of an attribute comes from a resource. The value in that resource can change.
StaticResource The value of an attribute comes from a resource. The value in that resource cannot change.
TemplateBinding Sets a property in a control template to a value.

The next table shows you some XAML markup extensions:

Extension Description
x:Null If you want to set an attribute to null, you must use the statement "{x:Null}".
x:Static Through this statement you can reference a static variable, a property, a constant or a value of an enumeration.
x:Type This statement is used to set a type in some situations, e.g. if you want to specify the TargetType of a style.

Language extensions:

XAML language extensions are a different way to extend the XAML dialect. Unlike the markup extensions, the language extensions haven't any equivalent classes. They are special statements for the XAML parser and compiler. They tell them to handle some sections in the XAML file in a special way (e.g. the x:Code statement). The following table shows some important XAML language extensions:

Attribute/Directive Description
x:Code Defines a code sections inside of a XAML file.
x:Key Sets a key name for a resource.
x:Name With this statement you can assign a name to an element, which haven't inherit the "Name" property, e.g. a GradientBrush.

Dependency properties

Dependency properties were introduced in the .NET Framework 3.0. They depend on the already known CLR properties and whose syntax, but they have an extended functionality:

  • An automatically update mechanism
  • An integrated validation
  • The possibility to define default values
  • The possibility to call a callback method, if the value has changed

There are different reasons to introduce the dependency properties. Many things in the WPF depends on the ability to notice, if some property values have changed in the system or in the application. For example, the notifications about changes and the automatic update of the dependent objects are one of these things. Specific features are only available for dependency properties, e.g.:

  • Data Binding
  • Styles
  • Animations

The value of an attribute depends therefore on the used style, data binding or animation. The concrete value must be calculated through the WPF. That's additional work for the framework and the reason, why not all properties are dependency properties.

Attached properties

Attached properties are a special type of dependency properties. They belong to an element (in most cases a layout container), but the values can be set through the child elements. This has important advantages:

  • The child elements aren't overloaded with information (e.g. there must be many layout container specific properties on each element).
  • The whole system is extendable.

A short example will clarify these advantages. If you want to add a TextBox element to a Grid for example, you would write it in C# like this:

  1. Grid.SetColumn(textBox, 0);
  2. Grid.SetRow(textBox, 1);

You must initialize the TextBox element (not shown in this example) and add the element to a specific column and row. This could only be done with the static methods "SetColumn" and "SetRow" of the Grid. In XAML you can write this like the following example:

  1. <Grid>
  2.   <TextBox Grid.Column="0" Grid.Row="1" Name="textBox" />
  3. </Grid>

As you can see, the usage of the attached properties simplifies the code. In order to notice that the properties "Column" and "Row" belong to the Grid, you must write the element (class) name before the property name.

Author: Fabian
Date: Thursday, 24. January 2008 20:03
Trackback: Trackback-URL Category: WPF

Feed: RSS 2.0

Leave a Reply