Introduction to XAML


XAML2XAML is a little bit similar to the very popular markup language HTML. XAML is nothing but the EXtensible Markup Language XML. XML is also similar to HTML since both markup languages share some base common properties and tag.

The only major difference we can determine is that HTML is a structure based markup language whereas XML is more generic (you can easily use XML for nearly any development work depending on your requirements).

In the past decades developers have used XML for things like:

  • Storing application settings
  • Data transfer
  • Communication medium

For using XML in your development work, first define a schema for declaring the proper name elements and their corresponding related attributes.

XAML is nothing but a special usage of XML. XAML has something to do with defining a user interface in our development work interface. So in this fashion it seems very similar to HTML, but there is a big difference between both of these. XAML is actually used to create instances of classes and sets of values of the properties and their related part.

Advantages of XAML

All you can do in XAML can also be done in code. XAML ist just another way to create and initialize objects. You can use WPF without using XAML. It’s up to you if you want to declare it in XAML or write it in code. Declare your UI in XAML has some advantages:

  • XAML code is short and clear to read
  • Separation of designer code and logic
  • Graphical design tools like Expression Blend require XAML as source.
  • The separation of XAML and UI logic allows it to clearly separate the roles of designer and developer.

XAML vs. Code

As an example we build a simple StackPanel with a textblock and a button in XAML and compare it to the same code in C#.

    <TextBlock Margin="20">Welcome to the World of XAML</TextBlock>
    <Button Margin="10" HorizontalAlignment="Right">OK</Button>

The same expressed in C# will look like this:

// Create the StackPanel
StackPanel stackPanel = new StackPanel();
this.Content = stackPanel;
// Create the TextBlock
TextBlock textBlock = new TextBlock();
textBlock.Margin = new Thickness(10);
textBlock.Text = "Welcome to the World of XAML";
// Create the Button
Button button = new Button();
button.Margin= new Thickness(20);
button.Content = "OK";

As you can see is the XAML version much shorter and clearer to read. And that’s the power of XAMLs expressiveness.

Properties as Elements

Properties are normally written inline as known from XML <Button Content="OK" />. But what if we want to put a more complex object as content like an image that has properties itself or maybe a whole grid panel? To do that we can use the property element syntax. This allows us to extract the property as an own child element.

     <Image Source="Images/OK.png" Width="50" Height="50" />

Implicit Type conversion

A very powerful construct of WPF are implicit type converters. They do their work silently in the background. When you declare a BorderBrush, the word “Blue” is only a string. The implicit BrushConverter makes a System.Windows.Media.Brushes.Blue out of it. The same regards to the border thickness that is beeing converted implicit into a Thickness object. WPF includes a lot of type converters for built-in classes, but you can also write type converters for your own classses.

<Border BorderBrush="Blue" BorderThickness="0,10">

Markup Extensions

Markup extensions are dynamic placeholders for attribute values in XAML. They resolve the value of a property at runtime. Markup extensions are surrouded by curly braces (Example: Background="{StaticResource NormalBackgroundBrush}"). WPF has some built-in markup extensions, but you can write your own, by deriving from MarkupExtension. These are the built-in markup extensions:

  • Binding
    To bind the values of two properties together.
  • StaticResource
    One time lookup of a resource entry
  • DynamicResource
    Auto updating lookup of a resource entry
  • TemplateBinding
    To bind a property of a control template to a dependency property of the control
  • x:Static
    Resolve the value of a static property.
  • x:Null
    Return null

The first identifier within a pair of curly braces is the name of the extension. All preciding identifiers are named parameters in the form of Property=Value. The following example shows a label whose Content is bound to the Text of the textbox. When you type a text into the text box, the text property changes and the binding markup extension automatically updates the content of the label.

<TextBox x:Name="textBox"/>
<Label Content="{Binding Text, ElementName=textBox}"/>


At the beginning of every XAML file you need to include two namespaces.
The first is It is mapped to all wpf controls in System.Windows.Controls.
The second is it is mapped to System.Windows.Markup that defines the XAML keywords.
The mapping between an XML namespace and a CLR namespace is done by the XmlnsDefinition attribute at assembly level. You can also directly include a CLR namespace in XAML by using the clr-namespace: prefix.

<Window xmlns="" xmlns:x="">

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s