Almost every XAML file begins with two namespaces.
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
These namespaces tell the XAML compiler where to find different types.
Default Namespace #
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
This namespace contains all the standard WPF controls.
Examples:
- Button
- Grid
- TextBox
- TextBlock
- Label
- ListView
- StackPanel
- DockPanel
Because it’s the default namespace, you can simply write:
<Button />
instead of
<presentation:Button />
x Namespace #
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
The second namespace is:
It provides XAML-specific functionality.
Common usages include:
x #
Links the XAML file with its C# code-behind.
x:Class="MyApplication.MainWindow"
x #
Assigns a name so the control can be accessed in C#.
<Button x:Name="btnSave"/>
In C#:
btnSave.Content = "Saved";
x #
Used inside resource dictionaries.
<SolidColorBrush x:Key="PrimaryColor"
Color="Blue"/>
x #
Accesses static members.
{x:Static SystemColors.HighlightBrush}
