The Root Elements
- <Window x:Class="HelloXAML.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="350" Width="525">
- </Window>
- <Page
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- x:Class="WPFApp.Page1"
- x:Name="Page"
- WindowTitle="Page"
- FlowDirection="LeftToRight"
- Width="640" Height="480"
- WindowWidth="640" WindowHeight="480">
- </Page>
- <UserControl x:Class="HelloXAML.Page"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Width="400" Height="300">
- </UserControl>
XAML Namespaces
- <Window
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- …… >
- </Window>
- x:Key: Sets a unique key for each resource in a ResourceDictionary.
- x:Class: Class name provides code-behind for a XAML page.
- x:Name: Unique run-time object name for the instance that exists in run-time code after an object element is processed.
- x:Static: Enables a reference that returns a static value that is not otherwise a XAML-compatible property.
- x:Type: Constructs a Type reference based on a type name.
Elements and Attributes
- <Button Content="Click Me" Width="200" Height="50"
- Background="Orange" Foreground="Blue"
- FontSize="20" FontFamily="Georgia" FontWeight="Bold"
- x:Name="ClickButton">
- </Button>
Content Property
- <Button Height="50" Margin="10,10,350,310" Content="Hello XAML" />
- Here is an alternative way to set the Content property of a Button.
- <Button Height="50" Margin="10,10,350,310">Hello XAML</Button>

- <Button Height="80" Margin="10,80,300,170">
- <Rectangle Height="60" Width="120" Fill="Green"/>
- </Button>
- <Button Margin="10,201,100,40">
- <StackPanel Orientation="Horizontal">
- <Ellipse Height="60" Width="60" Fill="Red"/>
- <TextBlock TextAlignment="Center"><Run Text=" Red Circle"/></TextBlock>
- <TextBlock TextAlignment="Center"><Run Text=" "/></TextBlock>
- <Rectangle Height="60" Width="120" Fill="Green"></Rectangle>
- <TextBlock TextAlignment="Center"><Run Text=" Green Rectangle"/></TextBlock>
- </StackPanel>
- </Button>
- <Window x:Class="ContentPropertySample.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="400" Width="500" >
- <Grid x:Name="ParentGrid">
- <Button Height="50" Margin="10,10,350,310" Content="Hello XAML" />
- <Button Height="80" Margin="10,80,300,170">
- <Rectangle Height="60" Width="120" Fill="Green"></Rectangle>
- </Button>
- <Button Margin="10,201,100,40">
- <StackPanel Orientation="Horizontal">
- <Ellipse Height="60" Width="60" Fill="Red"/>
- <TextBlock TextAlignment="Center"><Run Text=" Red Circle"/></TextBlock>
- <TextBlock TextAlignment="Center"><Run Text=" "/></TextBlock>
- <Rectangle Height="60" Width="120" Fill="Green"></Rectangle>
- <TextBlock TextAlignment="Center"><Run Text=" Green Rectangle"/></TextBlock>
- </StackPanel>
- </Button>
- </Grid>
- </Window>
- // Button with string content
- Button helloButton = new Button();
- helloButton.Margin = new Thickness(10,10,350,310);
- helloButton.Content = "Hello XAML";
- // Button with a UIElement
- Button buttonWithRectangle = new Button();
- buttonWithRectangle.Height = 80;
- buttonWithRectangle.Margin = new Thickness(10, 80, 300, 170);
- // Create a Rectangle
- Rectangle greenRectangle = new Rectangle();
- greenRectangle.Height = 60;
- greenRectangle.Width = 120;
- greenRectangle.Fill = Brushes.Green;
- // Set Rectangle as Button.Content
- buttonWithRectangle.Content = greenRectangle;
- // Button with a Container, StackPanel
- Button buttonWithStackPanel = new Button();
- buttonWithStackPanel.Margin = new Thickness(10, 10, 350, 310);
- // Create a StackPanel and set its orinetation to horizontal
- StackPanel stackPanel = new StackPanel();
- stackPanel.Orientation = Orientation.Horizontal;
- // Create an Ellipse
- Ellipse redEllipse = new Ellipse();
- redEllipse.Width = 60;
- redEllipse.Height = 60;
- redEllipse.Fill = Brushes.Red;
- // Add to StackPanel
- stackPanel.Children.Add(redEllipse);
- // Create a TextBlock
- TextBlock textBlock1 = new TextBlock();
- textBlock1.TextAlignment = TextAlignment.Left;
- textBlock1.Text = "Red Circle";
- // Add to StackPanel
- stackPanel.Children.Add(textBlock1);
- // Create a TextBlock
- TextBlock space = new TextBlock();
- space.TextAlignment = TextAlignment.Center;
- space.Text = " ";
- // Add to StackPanel
- stackPanel.Children.Add(space);
- // Create a Rectangle
- Rectangle greenRectangle2 = new Rectangle();
- greenRectangle2.Height = 60;
- greenRectangle2.Width = 120;
- greenRectangle2.Fill = Brushes.Green;
- // Add to StackPanel
- stackPanel.Children.Add(greenRectangle2);
- // Create a TextBlock
- TextBlock textBlock2 = new TextBlock();
- textBlock2.TextAlignment = TextAlignment.Left;
- textBlock2.Text = "Green Rectangle";
- // Add to StackPanel
- stackPanel.Children.Add(textBlock2);
- // Set StackPaenl as Button.Content
- buttonWithStackPanel.Content = stackPanel;
- // Add dynamic button controls to the Window
- ParentGrid.Children.Add(helloButton);
- ParentGrid.Children.Add(buttonWithRectangle);
- ParentGrid.Children.Add(buttonWithStackPanel);
XAML Events
- <Grid >
- <Button x:Name="HelloButton" Content="Click Me"
- Width="150" Height="40" Margin="11,10,357.667,280.667"
- FontSize="16" />
- <TextBlock x:Name="HelloTextBlock" Width="400" Height="100"
- Margin="10,57,208.667,163.667" Background="LightGray"
- FontSize="30" Foreground="Orange"/>
- </Grid>
- <Button x:Name="HelloButton" Content="Click Me"
- Width="150" Height="40" Margin="11,10,357.667,280.667"
- FontSize="16" Click="HelloButton_Click" />
- void HelloButton_Click(object sender, RoutedEventArgs e)
- {
- HelloTextBlock.Text = "HelloButton is clicked.";
- }
Recommended
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.

Junaid ShahidPosted Jun 25, 2019, 2:05 AM
This is nice piece of content, I'm working on WPF and it's fun to work with XAML and C# Dynamic controls which auto generate XAML in runtime. I'm looking for to customize WPF window title background and text align properties can you figure-out them? In XAML or in C#.
Hitanshi MehtaPosted Nov 20, 2018, 3:13 PM
Nice article!!!!!
Mahesh ChandPosted May 9, 2007, 9:45 AM
Yes Kadir. Go ahead and link back to the original link in the end of your page. Best, Mahesh
Kadir CamogluPosted May 9, 2007, 1:48 AM
Thank you for this good job. It is very compherensible and well-designed. Can I translate it in Turkish for Turkish developers?