#101 – What Visual Studio Does with Your XAML

Given that XAML is just a declarative representation of the objects used by your WPF application, the objects need to be instantiated at runtime.

Here’s the process (e.g. for MainWindow.xaml):

  • While you work in designer, VS2010 automatically generates partial class (e.g. MainWindow.g.i.cs), including:
    • Reference variables for named elements in XAML  (e.g. myButton)
    • Code to instantiate XAML objects at runtime
  • When you build the project
    • All code (yours and VS2010-generated) is compiled
    • XAML is compiled into BAML (binary), stored in .baml file
    • All BAML files combined into single .g.resources file (e.g. MyApp.g.resources)
    • Executable is built, embedding the .g.resources file as a resource
  • At runtime
    • Window constructor calls InitializeComponent
    • InitializeComponent (in generated code) calls Application.LoadComponent, passing URI identifying XAML
    • LoadComponent loads binary XAML from embedded resource, creates all objects
    • As BAML is read, window’s IComponentConnector.Connect method is called, which hooks up local object references to the created objects and attaches event handlers

#100 – Loose XAML Files

A loose XAML file is a file containing XAML that has no associated code-behind and is not compiled into an application.  A loose XAML file can be loaded into Internet Explorer, where the user interface will be rendered.

Some rules about loose XAML files:

  • The top-level element cannot be a Window, but could be a Page or other GUI element (e.g. StackPanel).
  • The top-level element must have any required namespaces defined
  • The top-level element cannot have a x:Class attribute
  • No elements in the file can have event handlers
  • The only browser supported is Internet Explorer (Firefox can render loose XAML with WPF plug-in)

#99 – Creating Custom Objects in XAML

In XAML, you normally create elements that come from the standard WPF and XAML namespaces.  But you can also create objects that are instances of your own custom classes, or standard .NET classes in other namespaces.

You start by including a new namespace attribute, to indicate the namespace and assembly where your class can be found.  In the following example, we set up the m: prefix as pointing to the namespace MyStuff, which is in the assembly MyStuffLib.dll.


Now we can instantiate objects from the MyStuff namespace.  Assuming that MyStuff includes a Movie class, we could create an instance of a Movie in a window’s resource dictionary:

     <m:Movie x:Key="movie1" Title="Dances With Wolves" Director="Kevin Costner"/>

One reason for doing this would be so that we could then use data binding to bind one or more controls to the custom object.

#98 – How Attached Properties Work in WPF

Attached properties in XAML allow attaching a property value to an object that is of a different type than the type where the property is defined.  (E.g. Grid.Row property can be set for a Button).

When the XAML parser encounters an attached property, it calls a static method on the class where the property is defined.  So setting a value for Grid.Row on a Button is equivalent to:

 Grid.SetRow(myButton, 1);

In WPF, attached properties are often implemented using dependency properties.  The object on which the property is being set (e.g. Button) inherits from DependencyObject, which means that it can store a collection of dependency properties.

When dependency properties are used for storing the values of attached properties, the static setter method in turn just calls the SetValue method on the object passed in to it.  So the method listed above would be implemented as:

 myButton.SetValue(Grid.RowProperty, 1);

#97 – Attached Properties

In XAML, attribute syntax is normally used to set the value of a property on an object.  But through the use of attached properties, you can set a value for a property that comes from another object.

Attached property syntax follows the form TypeName.PropertyName, where the type named is the type where the property is defined.

In the example below, we have two Button controls contained in a grid.  To indicate their position in the grid, we specify Grid.Row and Grid.Column properties for each.  The buttons don’t have Row and Column properties and neither does the Grid .  But the Grid does have Row and Column as attached properties–meaning that it can apply the properties to other objects, indicating where those objects should appear in the grid.

     <Button Grid.Row="1" Grid.Column="0" Content="Dee" Height="23" Name="button1" Width="75" />
     <Button Grid.Row="1" Grid.Column="1" Content="Dum" Height="23" Name="button2" Width="75" />

#96 – Common XAML Namespace Attributes

The XAML namespace is referenced in all XAML files used in WPF.  This namespace provides some basic elements used when parsing XAML.

The XAML namespace uses the x: prefix by default in WPF XAML files.

Here are the most common directives from the XAML namespace that can be used as attributes in your XAML:

  • x:Class – Defines the name of the class in code-behind that provides implementation for the element.
  • x:Key – Uniquely identifies an element in a resource dictionary.  The value must be a unique string in the dictionary.
  • x:Name – Provides a unique name for the element so that it can be referenced from code-behind
  • x:Null – Sets a property value to null
  • x:Shared – Allows users of an element in a resource dictionary to avoid sharing the same instance of the resource
  • x:Static – Allows setting a property value to a static property, a constant, or an enumerated value.

See also

#95 – x:Name vs. Name

You’ll typically see a Name property on XAML elements.  This property can be used in your code-behind, as a reference to the object being created.

 <Button Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" />
 <Button Content="Button" Height="23" Name="button2" Width="75" />

But you might also see x:Name being used (attribute syntax) in XAML.

 <Viewport3D Name="viewport1">
     <ModelVisual3D x:Name="visual1">

The Name property can be used with elements that inherit a Name property from their base class (e.g. FrameworkElement).  But for classes that don’t define a Name property or inherit from a class that does, you must use the x:Name property if you want to reference the object from your code-behind.

In general, Name and x:Name are interchangeable.  The former is an actual property on the class and the latter is a directive that comes from the default x: namespace and is used by the XAML parser.

#94 – Naming Elements

If you use drag and drop in Visual Studio 2010 to create new elements and include them in the underlying XAML file, each element is automatically given a name:

 <Button Content="Button" Height="23" Name="button1" Width="75" />
 <Button Content="Button" Height="23" Name="button2" Width="75" />

But setting this Name property is optional.  If you remove the Name properties, the elements will still get created and displayed properly.

Naming elements in XAML is required when you want to reference those elements in your code-behind.  For example, with the named buttons above, we can then write the following C# code:

 button1.Content = "Button 1";
 button2.Content = "This is button 2";

The name is used in code as a reference to the corresponding object created by the XAML parser.

#93 – Specifying Markup Extensions Using Property Element Syntax

Markup extensions are typically expressed directly as property values using braces–{, }. (Property attribute syntax).

 <Button Name="btnOne" Background="{StaticResource aliceBrush}" Content="Dum" Height="23" Width="75" />

But a markup extension can also be expressed using property element syntax.  The XAML fragment below is equivalent to the one listed above.

 <Button Name="btnOne" Content="Dum" Height="23" Width="75">
         <StaticResource ResourceKey="aliceBrush"/>

#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