Introduction to XAML
XAML stands for Extensible Application Markup Language.
Its a simple language based on XML to create and initialize .NET objects with hierarchical relations.
All classes in WPF have parameterless constructors and make excessive usage of properties.
Advantages of XAML
All you can do in XAML can also be done in code.
XAML is 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.
Properties as Elements
Properties are normally written inline as known from XML <Button Content="OK" />.
we can use the property element syntax. This allows us to extract the property as an own child element.
Syntax:
<Button>
<Button.Content>
<Image Source="Images/OK.png" Width="50" Height="50" />
</Button.Content>
</Button>
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.
Syntax:
<Border BorderBrush="Blue" BorderThickness="0,10">
</Border>
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
Syntax
<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.Windows.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 assembly level. You can also directly include a CLR namespace in XAML by using the clr-namespace: prefix.
Syntax:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Window>
XAML stands for Extensible Application Markup Language.
Its a simple language based on XML to create and initialize .NET objects with hierarchical relations.
All classes in WPF have parameterless constructors and make excessive usage of properties.
Advantages of XAML
All you can do in XAML can also be done in code.
XAML is 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.
Properties as Elements
Properties are normally written inline as known from XML <Button Content="OK" />.
we can use the property element syntax. This allows us to extract the property as an own child element.
Syntax:
<Button>
<Button.Content>
<Image Source="Images/OK.png" Width="50" Height="50" />
</Button.Content>
</Button>
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.
Syntax:
<Border BorderBrush="Blue" BorderThickness="0,10">
</Border>
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
Syntax
<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.Windows.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 assembly level. You can also directly include a CLR namespace in XAML by using the clr-namespace: prefix.
Syntax:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Window>