The book WPF 4.5 Unleaashed says: XAML is a relatively simple and general-purpose declarative programming language suitable for constructing and initializing objects. XAML is just XML, but with a set of rules about its elements and attributes and their mapping to objects, their properties, and the values of those properties (among other things). Using XAML in WPF projects is optional. Almost everything done with XAML can be done entirely in your favorite .NET procedural language (C# or VB) instead.
Elements and Attributes
The XAML specification defines rules that map .NET namespaces, types, properties, and events into XML namespaces, elements, and attributes. You can see this by examining the following simple (but complete) XAML file that declares a WPF Button and comparing it to the equivalent C# code:
<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Content="OK"/>
Here is the equivilent C# code:
System.Windows.Controls.Button b = new System.Windows.Controls.Button(); b.Content = "OK";
Declaring an XML element in XAML, known as an object element, is equivalent to instantiating the corresponding .NET object with a default constructor. Setting an attribute on the object element is equivalent to setting a property of the same name, called a property attribute, or hooking up an event handler of the same name, called an event attribute. For example, here’s an update to the Button that not only sets its Content property but also attaches an event handler to its Click event:
<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Content="OK" Click="button_Click"/>
System.Windows.Controls.Button b = new System.Windows.Controls.Button(); b.Click += new System.Windows.RoutedEventHandler(button_Click);b.Content = "OK";
This requires a method called button_Click to be defined somewhere, with the appropriate signature, which means that the XAML file can no longer be rendered standalone.
The XML namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation maps to the .NET namespace System.Windows.Controls. There is more to learn about namespaces that I won’t get into in this post.
You can put arbitrary content inside it; you’re not limited to just text. The following C# code embeds a simple square to make a Stop button like what might be found in a media player:
System.Windows.Controls.Button b = new System.Windows.Controls.Button(); System.Windows.Shapes.Rectangle r = new System.Windows.Shapes.Rectangle(); r.Width = 40; r.Height = 40; r.Fill = System.Windows.Media.Brushes.Black; b.Content = r; // Make the square the content of the Button
The Button’s Content property is of type System.Object, so it can easily be set to the 40×40 Rectangle object. How can you do the same thing in XAML with property attribute syntax? What kind of string could you possibly set Content to that is equivalent to the preceding Rectangle declared in C#? There is no such string, but XAML fortunately provides an alternative (and more verbose) syntax for setting complex property values: property elements. It looks like the following:
Property Attribute Syntax
<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <Button.Content> <Rectangle Height="40" Width="40" Fill="Black"/> </Button.Content> </Button>
There are two kinds of XAML elements: object elements and property elements. The Content property is now set with an XML element instead of an XML attribute, making it equivalent to the previous C# code. The period in Button.Content is what distinguishes property elements from object elements. Property elements always take the form TypeName.PropertyName, they are always contained inside a TypeName object element, and they can never have attributes of their own (with one exception—the x:Uid attribute used for localization).
Property Element Syntax
Many properties of an object can be set by using property element syntax. A property element looks like this:
<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Content="OK" Background="White"/>
below we are setting the properties with elements
<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <Button.Content>OK</Button.Content> <Button.Background>White</Button.Background> </Button>
The book WPF 4.5 Unleashed has, in Chapter 2, a few more topics to cover that I cover in this XAML Introduction series of posts. They are: Type Converters, Markup Extensions, Children of Object Elements, Mixing XAML With Procedural Code, XAML2009, and XAML Keywords.