What is XAML

XAML is a new descriptive programming language developed by Microsoft to write user interfaces for next-generation managed applications. XAML is the language to build user interfaces for Windows and Mobile applications that use Windows Presentation Foundation (WPF), UWP, and Xamarin Forms.
 
The purpose of XAML is simple, to create user interfaces using a markup language that looks like XML. Most of the time, you will be using a designer to create your XAML but you’re free to directly manipulate XAML by hand.
 
XAML uses the XML format for elements and attributes. Each element in XAML represents an object which is an instance of a type. The scope of a type (class, enumeration etc.) is defined as a namespace that physically resides in an assembly (DLL) of the .NET Framework library.
 
 
 
Similar to XML, a XAML element syntax always starts with an open angle bracket (<) and ends with a close angle bracket (>). Each element tag also has a start tag and an end tag. For example, a Button object is represented by the <Button> object element. The following code snippet represents a Button object element.
 
<Button></Button>
 
Alternatively, you can use a self-closing format to close the bracket.
 
<Button />
 
An object element in XAML represents a type. A type can be a control, a class or other objects defined in the framework library.
 

The Root Elements

 
Each XAML document must have a root element. The root element usually works as a container and defines the namespaces and basic properties of the element. Three most common root elements are <Windows />, <Page />, and <UserControl >. The <ResourceDirectory /> and <Application /> are other two root elements that can be used in a XAML file.
 
The Window element represents a Window container. The following code snippet shows a Window element with its Height, Width, Title and x:Name attributes. The x:Name attribute of an element represents the ID of an element used to access the element in the code-behind. The code snippet also sets xmlns and xmlns:x attributes that represent the namespaces used in the code. The x:Class attribute represents the code-behind class name. 
  1. <Window x:Class="HelloXAML.MainWindow"  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4. Title="MainWindow" Height="350" Width="525">  
  5. </Window>  
The Page element represents a page container. The following code snippet creates a page container. The code also sets the FlowDirection attribute that represents the flow direct of the contents of the page. 
  1. <Page  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4. x:Class="WPFApp.Page1"  
  5. x:Name="Page"  
  6. WindowTitle="Page"  
  7. FlowDirection="LeftToRight"  
  8. Width="640" Height="480"  
  9. WindowWidth="640" WindowHeight="480">  
  10. </Page>  
The UserControl element represents a user control container. The following code snippet represents a user control container. 
  1. <UserControl x:Class="HelloXAML.Page"  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4. Width="400" Height="300">  
  5. </UserControl>  

XAML Namespaces

 
The part of the root element of each XAML are two or more attributes pre-fixed with xmlns and xmlns:x.
  1. <Window  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4. …… >  
  5. </Window>  
The xmlns attribute indicates the default XAML namespace so the object elements in used in XAML can be specified without a prefix. Additionally, the x: prefix is used with more than just the namespace. Here are some common x:prefix syntaxes that are used in XAML. 
  1. x:Key: Sets a unique key for each resource in a ResourceDictionary.
  2. x:Class: Class name provides code-behind for a XAML page.
  3. x:Name: Unique run-time object name for the instance that exists in run-time code after an object element is processed.
  4. x:Static: Enables a reference that returns a static value that is not otherwise a XAML-compatible property.
  5. x:Type: Constructs a Type reference based on a type name. 

Elements and Attributes

 
A type in WPF or Windows RT is represented by an XAML element. The <Page> and <Button> elements represent a page and a button control respectively. The XAML Button element listed in the following code represents a button control.
 
<Button />
 
Each of the elements such as <Page> or <Button> have attributes that can be set within the element itself. An attribute of an element represents a property of the type. For example, a Button has Height, Width, Background and Foreground properties that represent the height, width, foreground color and background color of the button respectively. The Content property of the Button represents the text of a button control. The x:Name property represents the unique ID of a control that may be used to access a control in the code behind.
 
The following code snippet sets the ID, height, width, background color, foreground color, font name and size and content of a button control. 
  1. <Button Content="Click Me" Width="200" Height="50"  
  2. Background="Orange" Foreground="Blue"  
  3. FontSize="20" FontFamily="Georgia" FontWeight="Bold"  
  4. x:Name="ClickButton">  
  5. </Button>  

Content Property

 
Each XAML object element is capable of displaying different content types. XAML provides a special property called Content that works to display the content of the element depending on the element capabilities. For example, a Content property of a Button can be a set to a string, an object, a UIElement, or even and container. However, the Content property of a ListBox is set using the Items property.
 
Note: Some XAML object elements may not have the Content property available directly. It must be set through a property.
 
The code snippet in the following code creates a Button control and sets its Content property to a string “Hello XAML”.
  1. <Button Height="50" Margin="10,10,350,310" Content="Hello XAML" />  
  2. Here is an alternative way to set the Content property of a Button.  
  3. <Button Height="50" Margin="10,10,350,310">Hello XAML</Button>  
The output of the above code looks like the following.
 
XAML Button
 
A Button element can display other child elements as its content. The following code listed sets a Rectangle element as the content of the Button.
  1. <Button Height="80" Margin="10,80,300,170">  
  2. <Rectangle Height="60" Width="120" Fill="Green"/>  
  3. </Button>  
The output looks like the following.
 
XAML Rectangle 
 
Content property can also be a container or a parent element hosting child elements. The code listed below sets a StackPanel container with 5 child elements as the content of the Button.
  1. <Button Margin="10,201,100,40">  
  2. <StackPanel Orientation="Horizontal">  
  3. <Ellipse Height="60" Width="60" Fill="Red"/>  
  4. <TextBlock TextAlignment="Center"><Run Text=" Red Circle"/></TextBlock>  
  5. <TextBlock TextAlignment="Center"><Run Text=" "/></TextBlock>  
  6. <Rectangle Height="60" Width="120" Fill="Green"></Rectangle>  
  7. <TextBlock TextAlignment="Center"><Run Text=" Green Rectangle"/></TextBlock>  
  8. </StackPanel>  
  9. </Button>  
Here is the new output.
 
XAML Controls 
 
Here is the complete XAML code.
  1. <Window x:Class="ContentPropertySample.MainWindow"  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4. Title="MainWindow" Height="400" Width="500" >  
  5. <Grid x:Name="ParentGrid">  
  6. <Button Height="50" Margin="10,10,350,310" Content="Hello XAML" />  
  7. <Button Height="80" Margin="10,80,300,170">  
  8. <Rectangle Height="60" Width="120" Fill="Green"></Rectangle>  
  9. </Button>  
  10. <Button Margin="10,201,100,40">  
  11. <StackPanel Orientation="Horizontal">  
  12. <Ellipse Height="60" Width="60" Fill="Red"/>  
  13. <TextBlock TextAlignment="Center"><Run Text=" Red Circle"/></TextBlock>  
  14. <TextBlock TextAlignment="Center"><Run Text=" "/></TextBlock>  
  15. <Rectangle Height="60" Width="120" Fill="Green"></Rectangle>  
  16. <TextBlock TextAlignment="Center"><Run Text=" Green Rectangle"/></TextBlock>  
  17. </StackPanel>  
  18. </Button>  
  19. </Grid>  
  20. </Window>  
There is the final output.
 
 
 
As you can imagine from the preceding examples, you can pretty much host any user interface as the content of a XAML element.
 
The following code creates the preceding Button controls dynamically in the code and sets their Content properties to a string, a Rectangle and a StackPanel respectively.
  1. // Button with string content  
  2. Button helloButton = new Button();  
  3. helloButton.Margin = new Thickness(10,10,350,310);  
  4. helloButton.Content = "Hello XAML";  
  5. // Button with a UIElement  
  6. Button buttonWithRectangle = new Button();  
  7. buttonWithRectangle.Height = 80;  
  8. buttonWithRectangle.Margin = new Thickness(10, 80, 300, 170);  
  9. // Create a Rectangle  
  10. Rectangle greenRectangle = new Rectangle();  
  11. greenRectangle.Height = 60;  
  12. greenRectangle.Width = 120;  
  13. greenRectangle.Fill = Brushes.Green;  
  14. // Set Rectangle as Button.Content  
  15. buttonWithRectangle.Content = greenRectangle;  
  16. // Button with a Container, StackPanel  
  17. Button buttonWithStackPanel = new Button();  
  18. buttonWithStackPanel.Margin = new Thickness(10, 10, 350, 310);  
  19. // Create a StackPanel and set its orinetation to horizontal  
  20. StackPanel stackPanel = new StackPanel();  
  21. stackPanel.Orientation = Orientation.Horizontal;  
  22. // Create an Ellipse  
  23. Ellipse redEllipse = new Ellipse();  
  24. redEllipse.Width = 60;  
  25. redEllipse.Height = 60;  
  26. redEllipse.Fill = Brushes.Red;  
  27. // Add to StackPanel  
  28. stackPanel.Children.Add(redEllipse);  
  29. // Create a TextBlock  
  30. TextBlock textBlock1 = new TextBlock();  
  31. textBlock1.TextAlignment = TextAlignment.Left;  
  32. textBlock1.Text = "Red Circle";  
  33. // Add to StackPanel  
  34. stackPanel.Children.Add(textBlock1);  
  35. // Create a TextBlock  
  36. TextBlock space = new TextBlock();  
  37. space.TextAlignment = TextAlignment.Center;  
  38. space.Text = " ";  
  39. // Add to StackPanel  
  40. stackPanel.Children.Add(space);  
  41. // Create a Rectangle  
  42. Rectangle greenRectangle2 = new Rectangle();  
  43. greenRectangle2.Height = 60;  
  44. greenRectangle2.Width = 120;  
  45. greenRectangle2.Fill = Brushes.Green;  
  46. // Add to StackPanel  
  47. stackPanel.Children.Add(greenRectangle2);  
  48. // Create a TextBlock  
  49. TextBlock textBlock2 = new TextBlock();  
  50. textBlock2.TextAlignment = TextAlignment.Left;  
  51. textBlock2.Text = "Green Rectangle";  
  52. // Add to StackPanel  
  53. stackPanel.Children.Add(textBlock2);  
  54. // Set StackPaenl as Button.Content  
  55. buttonWithStackPanel.Content = stackPanel;  
  56. // Add dynamic button controls to the Window  
  57. ParentGrid.Children.Add(helloButton);  
  58. ParentGrid.Children.Add(buttonWithRectangle);  
  59. ParentGrid.Children.Add(buttonWithStackPanel);  

XAML Events

 
Windows controls have most of the common events such as Click, GotFocus, LostFocus, KeyUp, KeyDown, MouseEnter, MouseLeave, MouseLeftButtonDown, MouseRightButtonDown and MouseMove. An event in XAML has an event handler that is defined in the code-behind and the code is executed when the event is raised.
 
Let’s see this by an example.
 
Create a new WPF Application and add a Button and a TextBlock control to the Window. Position and format the control the way you like. My final code is listed below.
  1. <Grid >  
  2. <Button x:Name="HelloButton" Content="Click Me"  
  3. Width="150" Height="40" Margin="11,10,357.667,280.667"  
  4. FontSize="16" />  
  5. <TextBlock x:Name="HelloTextBlock" Width="400" Height="100"  
  6. Margin="10,57,208.667,163.667" Background="LightGray"  
  7. FontSize="30" Foreground="Orange"/>  
  8. </Grid>
We will now add the Button click event handler and write the code that will add some text to the TextBlock on the button click. As shown in the following code, add the click event hander called HelloButton_Click. 
  1. <Button x:Name="HelloButton" Content="Click Me"  
  2. Width="150" Height="40" Margin="11,10,357.667,280.667"  
  3. FontSize="16" Click="HelloButton_Click" />  
Now go to the code-behind and add the following code listing. This code is the HelloButton’s click event handler where we update the Text of the TextBlock. 
  1. void HelloButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3. HelloTextBlock.Text = "HelloButton is clicked.";  
  4. }  
Build and run the application and click on the button, the output will change to the following.
 
XAML Events 

Recommended

 
Here is a free Ebook download on XAML: Programming XAML 


Here are some XAML Frequenty Asked Questions:
 

What is XAML?

XAML is a new descriptive programming language developed by Microsoft to write user interfaces for next generation managed applications. 

Will XAML Replace C# and VB.NET Languages?

No. XAML complements procedural languages. You can think of XAML as ASP.NET. To write ASP.NET applications, you need both ASP.NET and C# (or VB.NET). Similar to ASP.NET, XAML provides user interfaces and C# (or VB.NET) is used as code-behind language. 

What is the Relationship between XAML and WPF? 

WPF is code behind XAML. WPF is a name of .NET API (a set of classes) provided to write user interfaces for XAML under the hood. 

What is the Relationship between WPF and C# or VB.NET? 

WPF is the API similar to Windows Forms or ASP.NET. When you use Windows Forms, you need a language such as C# or VB.NET. Similarly, to write WPF applications, you will have to use a language C# or VB.NET. 

Will XAML and WPF Replace Windows Forms? 

Yes and No. Today, programmers can choose from WPF and Windows Forms. Now Windows Forms will be the older way to write Windows user interfaces and WPF is the new way ;-). It's hard to say how long  it will take XAML + WPF models to replace Windows Forms. 

Will XAML Replace ASP.NET? 

No. ASP.NET is to build Web applications and XAML is to build Windows. They are two different programming frameworks. 

 


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.