Friday, 6 November 2009

XAML markup extensions

“Markup extensions are a XAML concept. In attribute syntax, curly braces ({ and }) indicate a markup extension usage. This usage directs the XAML processing to escape from the general treatment of attribute values as either a literal string or a directly string-convertible value.” (my italics)

See http://msdn.microsoft.com/en-us/library/ms752059.aspx

“When used to provide an attribute value, the syntax that distinguishes a markup extension to a XAML processor is the presence of the opening and closing curly braces ({ and }). The type of markup extension is then identified by the string token immediately following the opening curly brace.

When used in property element syntax, a markup extension is visually the same as any other element used to provide a property element value: a XAML element declaration that references the markup extension class as an element, enclosed within angle brackets (<>). ”

See http://msdn.microsoft.com/en-us/library/ms747254.aspx

XAML-defined markup extensions

There are a number of XAML extensions not specific to WPF. These are usually identified by the prefix x: which is an XML prefix used to map the XAML namespace.

Extension tag Description
x:Type Supplies the Type object for the named type. This is used most frequently in styles and templates.
x:Static Produces static values from value-type code entities that are not directly the type of a property's value, but can be evaluated to that type.
x:Null Specifies null as a value for a XAML property.
x:Array Provides support for creation of general arrays in XAML syntax, for cases where the collection support provided by base elements and control models is deliberately not used.

Nested extension syntax

Extensions can be nested. For example:

<Setter Property="Background"  Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />

{} Escape Sequence / Markup Extension

To escape curly braces precede with the {} escape sequence, especially where the first character of the property is an opening curly brace. For example:

<Setter Property="SomeProperty"  Value="{}{" />

The value of SomeProperty will be set to {.

“The {} escape sequence is frequently required when specifying an XML type that must include a namespace qualifier in a location where XAML markup extension might appear. This includes the beginning of an XAML attribute value, and within a markup extension, immediately after an equal-sign. The following example shows escapes for an XML namespace that appears at the beginning of a XAML attribute value.”

  <StackPanel.Resources>
    <DataTemplate DataType="{}{http://planetsNS}Planet" >
      <StackPanel Orientation="Horizontal">
        <TextBlock Width="100" Text="{Binding Path=Element[{http://planetsNS}DiameterKM].Value}" />
        <TextBlock Width="100" Text="{Binding Path=Attribute[Name].Value}" />
        <TextBlock Text="{Binding Path=Element[{http://planetsNS}Details].Value}" /> 
      </StackPanel>
    </DataTemplate>
  </StackPanel.Resources>

See http://msdn.microsoft.com/en-us/library/ms744986.aspx