blog

Home / DeveloperSection / Blogs / Introduction to XAML

Introduction to XAML

priyanka kushwaha2795 20-Mar-2015

In this post, I’m explaining about XAML.


What is XAML?

XAML, which stands for Extensible Application Markup language. It’s a simple language based on XML to create and initialize the .NET object with the hierarchical relation. It was originally invented for WPF it can be used to create any kind of object trees.

XAML Elements are .NET Classes

Each element in a XAML document refers to a .NET class. This means that both “<Window>” and “<Grid>” are .NET Classes.

Example:

This document includes only two elements- top-level window element, which represents the entire window, and the Grid, in which you can place all you control. Although you could use any top-level element. WPF application rely on just a few:

  • Window
  • Page (which is similar to window but used for navigable application)
  • Application (which defines application resources and startup setting)

 

XAML property-Element syntax

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.

<button>
         <button.Content>
                  < image source=”Image/OK.jpg” width=”50” Height=”50”>
         </button.Content>
</button>

 

Implicit type conversion

A very powerful construct of WPF is 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 being converted implicitly 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 classes.

<Border BorderBrush=”Blue” BorderThickness=”0,10”>
</Border>

Markup Extensions

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

1.       Binding:

To bind the values of two properties together.

2.       StaticResource:

On time lookup of a resource entry.

3.       DynamicResource

Auto updating lookup of a resource entry.

4.       TemplateBinding

To bind a property of a control template to a dependency property of the control.

5.       x:static

Resolve the value of a static property.

6.       x:null

Return null

Example:

<Textbox x:Name=”textbox”>
<Label Content=”{Binding Text, ElementName=textbox}”>

Namespaces:

At the beginning of every XAML file you need to include two namespaces:

The first is http://schemas.microsoft.com/winfx/2006/xaml/presentation. It is mapped to all WPF controls in System.Window.Controls.

The second is http://schemas.microsoft.com/winfx/2006/xaml 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 the assembly level.

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Window>

 

Mixing XAML with procedural Code

XAML-based windows store apps are a mix of XAML and procedural code. This section covers the two ways that XAML and code can be mixed together. Dynamically loading and parsing XAML yourself or leveraging the built-in support in Visual Studio projects.


Updated 22-Feb-2018

Leave Comment

Comments

Liked By