XAML Overview - M$ Tutorial

  • View

  • Download

Embed Size (px)


Microsoft tutorial about XAML - an XML dialect

Text of XAML Overview - M$ Tutorial

  • XAML Overview (WPF) http://msdn.microsoft.com/en-us/library/ms752059.aspx

    .NET Framework 4.5

    This topic describes the features of the XAML language and demonstrates how you can use XAML to write

    Windows Presentation Foundation (WPF) applications. This topic specifically describes XAML as

    implemented by WPF. XAML itself is a larger language concept than WPF.

    This topic contains the following sections.

    What is XAML?

    XAML Syntax in Brief

    Case and Whitespace in XAML

    Markup Extensions

    Type Converters

    XAML Root Elements and XAML Namespaces

    Custom Prefixes and Custom Types in XAML

    Events and XAML Code-Behind

    XAML Named Elements

    Attached Properties and Attached Events

    Base Types and XAML

    XAML Security

    Loading XAML from Code

    What's Next

    Related Topics

    What is XAML?

    XAML is a declarative markup language. As applied to the .NET Framework programming model, XAML

    simplifies creating a UI for a .NET Framework application. You can create visible UI elements in the

    declarative XAML markup, and then separate the UI definition from the run-time logic by using code-

    behind files, joined to the markup through partial class definitions. XAML directly represents the

    instantiation of objects in a specific set of backing types defined in assemblies. This is unlike most other

    markup languages, which are typically an interpreted language without such a direct tie to a backing type

    system. XAML enables a workflow where separate parties can work on the UI and the logic of an

    application, using potentially different tools.

    When represented as text, XAML files are XML files that generally have the .xaml extension. The files can

    be encoded by any XML encoding, but encoding as UTF-8 is typical.

  • The following example shows how you might create a button as part of a UI. This example is just intended

    to give you a flavor of how XAML represents common UI programming metaphors (it is not a complete



    XAML Syntax in Brief

    The following sections explain the basic forms of XAML syntax, and give a short markup example. These

    sections are not intended to provide complete information about each syntax form, such as how these are

    represented in the backing type system. For more information about the specifics of XAML syntax for each

    of the syntax forms introduced in this topic, see XAML Syntax In Detail.

    Much of the material in the next few sections will be elementary to you, if you have previous familiarity

    with the XML language. This is a consequence of one of the basic design principles of XAML. XAML The

    XAML language defines concepts of its own, but these concepts work within the XML language and

    markup form.

    XAML Object Elements

    An object element typically declares an instance of a type. That type is defined in the assemblies that

    provide the backing types for a technology that uses XAML as a language.

    Object element syntax always starts with an opening angle bracket (). You can instead use a self-closing form that does

    not have any content, by completing the tag with a forward slash and closing angle bracket in succession

    (/>). For example, look at the previously shown markup snippet again:


    This specifies two object elements: (with content, and a closing tag later),

    and (the self-closing form, with several attributes). The object

    elements StackPanel and Button each map to the name of a class that is defined by WPF and is part of

    the WPF assemblies. When you specify an object element tag, you create an instruction for XAML

    processing to create a new instance. Each instance is created by calling the default constructor of the

    underlying type when parsing and loading the XAML.

    Attribute Syntax (Properties)

    Properties of an object can often be expressed as attributes of the object element. An attribute syntax

    names the property that is being set in attribute syntax, followed by the assignment operator (=). The

    value of an attribute is always specified as a string that is contained within quotation marks.

  • Attribute syntax is the most streamlined property setting syntax and is the most intuitive syntax to use for

    developers who have used markup languages in the past. For example, the following markup creates a

    button that has red text and a blue background in addition to display text specified as Content.


    Property Element Syntax

    For some properties of an object element, attribute syntax is not possible, because the object or

    information necessary to provide the property value cannot be adequately expressed within the quotation

    mark and string restrictions of attribute syntax. For these cases, a different syntax known as property

    element syntax can be used.

    The syntax for the property element start tag is . Generally, the content of that

    tag is an object element of the type that the property takes as its value . After specifying content, you

    must close the property element with an end tag. The syntax for the end tag

    is .

    If an attribute syntax is possible, using the attribute syntax is typically more convenient and enables a

    more compact markup, but that is often just a matter of style, not a technical limitation. The following

    example shows the same properties being set as in the previous attribute syntax example, but this time by

    using property element syntax for all properties of the Button.


    This is a button

    Collection Syntax

    The XAML language includes some optimizations that produce more human-readable markup. One such

    optimization is that if a particular property takes a collection type, then items that you declare in markup

    as child elements within that property's value become part of the collection. In this case a collection of

    child object elements is the value being set to the collection property.

    The following example shows collection syntax for setting values of the GradientStops property:


  • XAML Content Properties

    XAML specifies a language feature whereby a class can designate exactly one of its properties to be the

    XAML content property. Child elements of that object element are used to set the value of that content

    property. In other words, for the content property uniquely, you can omit a property element when

    setting that property in XAML markup and produce a more visible parent/child metaphor in the markup.

    For example, Border specifies a content property of Child. The following two Border elements are treated

    identically. The first one takes advantage of the content property syntax and omits

    the Border.Child property element. The second one shows Border.Child explicitly.


    As a rule of the XAML language, the value of a XAML content property must be given either entirely

    before or entirely after any other property elements on that object element. For instance, the following

    markup does not compile:

    I am a


    blue button

    For more information about this restriction on XAML content properties, see the "XAML Content

    Properties" section of XAML Syntax In Detail.

    Text Content

    A small number of XAML elements can directly process text as their content. To enable this, one of the

    following cases must be true:

    The class must declare a content property, and that content property must be of a type

    assignable to a string (the type could be Object). For instance,

    any ContentControl uses Content as its content property and it is type Object, and this supports

    the following usage on a practical ContentControl such as aButton: Hello.

    The type must declare a type converter, in which case the text content is used as initialization text

    for that type converter. For example, Blue. This case is less common in


    The type must be a known XAML language primitive.

    Content Properties and Collection Syntax Combined

  • Consider this example:


    First Button

    Second Button

    Here, each Button is a child element of StackPanel. This is a streamlined and intuitive markup that omits

    two tags for two different reasons.

    Omitted StackPanel.Children property element: StackPanel derives

    from Panel. Panel definesPanel.Children as its XAML content property.

    Omitted UIElementCollection object element: The Panel.Children property takes the

    typeUIElementCollection, which implements IList. The collection's element tag can be omitted,

    based on the XAML rules for processing collections such as IList. (In this

    case, UIElementCollection actually cannot be instantiated because it does not expose a default

    constructor, and that is why the UIElementCollectionobject element is shown commented out).


    First Button

    Second Button

    Attribute Syntax (Events)

    Attribute syntax can also be used for members that are events rather than properties. In this case, the

    attribute's name is the name of the event. In the WPF implementation of events for XAML, the attribute's

    value is the name of a handler that implements that event's delegate. For example, the following markup

    assigns a handler for the Click event to a Button created in markup:


    Click Me!

    There is more to events and XAML in WPF tha