#107 – Markup Extensions in the XAML Namespace

Some markup extensions are part of the extensions to XAML added for WPF (e.g. StaticResource).  But some markup extensions are part of the XAML vocabulary itself, typically prefixed with x:.  They are listed below:

  • x:Array – Allows including an array of objects in XAML
  • x:Null – A null value
  • x:Reference – A reference to another element defined in XAML  (XAML 2009)
  • x:Static – References a static element in code, e.g. value of a static property
  • x:Type – Specifies a .NET type

#92 – Markup Extensions

When property values are specified in XAML, the property can get its value in one of three different ways:

  • Property assigned a literal string value
  • Property assigned a value by a type converter, converted from a literal string
  • Property assigned based on a markup extension

A markup extension is special syntax that dictates where a property should get its value.  Markup extensions are specified using braces–{, }.

 <!-- Background property set using type converter -->
 <Button Background="Red" Content="Dum" Height="23" Width="75" />
 <!-- Background property set using the StaticResource markup extension -->
 <Button Background="{StaticResource blueBrush}" Content="Dee" Height="23" Width="75" />

The most common markup extensions that you’ll encounter in WPF include:

  • StaticResource – set property to object in resource dictionary
  • DynamicResource – set property to resource, loading at run-time
  • Binding – set property using data binding
  • TemplateBinding – placeholder in a template, value comes from the object using the template