Jaa


Xaml terminology & intro

One of the things I don't think we've done that good a job on in WPF is using a consistent set of terminology -- the product cycle has been long enough that a lot of things have gone through multiple names, and we often use them interchangeably. That's not a problem for those who know WPF well, but for the beginners it can be fairly confusing. Since I've been spending a lot of time lately on xaml issues, figured I would share this little write up on the official xaml terminology:

Xaml is an XML-based markup language that provides a clean mapping between markup and programmatic access.  Lets take an example:
    <Canvas Width="400" Height="400"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="root" Loaded="javascript:root_Loaded"
     >
    
        <Rectangle x:Name="rectangle" Canvas.Top="0" Canvas.Left="0"
            Height="20" Width="20">
            <Rectangle.Fill>
                <SolidColorBrush Color="green"/>
            </Rectangle.Fill>
        </Rectangle>
    </Canvas>

The “root element” is the first XML element in the xaml file.  (There may be comments, whitespace, and processing instructions before the root element) The root element typically defines some XML namespaces to use throughout the document.  In the above example, <Canvas> is the root element.

An “object element” is an element that instructs xaml to create an object.  Object elements have the same name as a class.  In the example above, <Canvas> and <Rectangle> are object elements.

“Attributes” are the foo=”bar” things.  Xaml supports three kinds of attributes – “property attributes” correspond to a property on the object, e.g. Height.  “Event attributes” correspond to an event (e.g. Loaded).  “Directive attributes” tell the parser about things that don’t have a programmatic equivalent, e.g. xmlns or x:Name.  “attached property attributes” are special kind of property attribute where the name contains a dot, e.g. Canvas.Top.

“Property elements” are elements that represent properties on the parent element (which is an object element).  In the example above, <Rectangle.Fill> is a property element of <Rectangle>, and represents Rectangle’s Fill property.

The above xaml roughly corresponds to this pseudo-code:

Canvas root = new Canvas();
root.Loaded += new EventHandler(root_Loaded);
root.Height = 400;
root.Width = 400;

Rectangle rectangle = new Rectangle();
rectangle.SetValue(“Canvas.Top”, 0);
rectangle.SetValue(“Canvas.Left”, zero);
rectangle.Height = 20;
rectangle.Width = 20;
SolidColorBrush temporary = new SolidColorBrush();
temporary.Color = green;
rectangle.Fill = temporary;
root.AddChild(rectangle);

An element’s “content” are the elements and text inside it (in the example above, <Rectangle> is the content of <Canvas>, and <Rectangle.Fill> is the content of <Rectangle>).  An element’s “content model” is all allowable content.

The phrase “object content” means content other than property elements, and “object content model” means what non-property elements is allowable.  Note that object content includes text and markup extensions (in element syntax) as well as object elements.

And “markup extension” is a special syntax that creates objects/values, but not according to the normal rules of object elements, property attributes, etc..  Markup extensions support both attribute syntax and element syntax.  The following is an example of a markup extension called StaticResource using attribute syntax:
 <Rectangle SomeProperty=”{StaticResource foo}”/>

The following is an example of the StaticResource markup extension using element syntax:
        <Rectangle x:Name="rectangle" Canvas.Top="0" Canvas.Left="0"
            Height="20" Width="20">
            <Rectangle.Fill>
        <StaticResource ResourceKey="foo"/>
            </Rectangle.Fill>
        </Rectangle>

xaml also supports an object element with all text content, which is run through a type converter, this is called “object element with initialization text”.  An example:

<sys:Int32>100</sys:Int32>

xaml also supports a related syntax where a type converter string can appear directly inside a property element, this is called “property element with initialization text”.  An example:
 
 <Canvas>
     <Canvas.Height>100</Canvas.Height>
 </Canvas>

Comments

  • Anonymous
    August 29, 2006
    &lt;p&gt;&lt;a href=&quot;http://blogs.msdn.com/nickkramer/archive/2006/08/29/729224.aspx&quot;&gt;htt
  • Anonymous
    August 31, 2006
    Really extremely good article. There are almost nothing so much clean for introduction and definitions. They might read 1000 pages from books and MSDN and still have questions on that terms. Your article solve this problem.

    Thank you, Nick!
  • Anonymous
    September 05, 2006
    Nick,

    Superb 'contextual and to the point' definition of terms. Contains (1) an example, (2) a precise definition, (3) an enumeration of possibilities, (4) eventual exceptions and/or extensions ... Wish many other WPF concepts would be exposed with such methodology and clarity ... So much better than some existing documentation ...

    Marc