Beginning XAML

This article provides a simple way to try XAML without C#. This is for anyone unfamiliar with XAML that wants to get an idea of what it is and how it works. This article only explains XAML, not WPF or even C#. You don't even need special software for this article; you can use just Notepad and Internet Explorer if you wish. You can use most any text editor, including Visual Studio if you wish. I won't provide the concepts; you can get that elsewhere. The intent is to allow you to have a little fun playing with XAML to become familiar with it.

It is possible to create a XAML file and then see the results in Internet Explorer simply by browsing to the file. The trick is to use the "Page" tag as the root in the XAML file. Usually when we create a window using XAML with WPF, the root tag is "Window". If you try to browse a XAML file with the root tag as "Window" in IE directly then it won't work. The XAML in this article will do nothing except create the corresponding window; there is no "code behind" so if you click on a button, nothing happens. The XAML in this article is not good for anything except for learning purposes.
 
So without further explanation, we will dive in. Using any text editor, put the following in a text file:
  1. <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  2.       xmlns:sys="clr-namespace:System;assembly=mscorlib"  
  3.       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.       >  
  5.     <Label Content="This is a Label" />  
  6. </Page>  

Then save the file with the extension "xaml". If you are unsure of what I mean then please download the attached zip file and look at the file "Skeleton.xaml". When you browse to the file in IE you will get:

Note that the Label tag created a Label. So now for the fun part. Try adding the following tag after the Label tag:

  1. <Button Content="Button" Width="75" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top"/>  

Then refresh the page if you still have the file open in IE or browse to the file using IE. You will get:

That is not fun. So we will use a Grid tag and put the other stuff in it. So put the following within the "Page" tag (I am excluding the "Page" tag from the following):

  1. <Grid>  
  2. <Label Content="This is a Label" Margin="12,12,0,0" />  
  3. <Button Content="Button" Margin="12,48,0,0" Width="75" Height="23"  
  4. HorizontalAlignment="Left" VerticalAlignment="Top" />  
  5. </Grid>  

You can play around with that. The following shows a bunch of stuff; give this a try (there should be nothing else in the file except the Page tag):

  1. <DockPanel>  
  2. <Menu Height="25" DockPanel.Dock="Top">  
  3. <MenuItem Header="_File">  
  4. <MenuItem Header="_Open"/>  
  5. <MenuItem Header="_Save"/>  
  6. <MenuItem Header="Save_As"/>  
  7. </MenuItem>  
  8. </Menu>  
  9. <StatusBar Height="25" Margin="2,2,0,0" Name="statusBar1" DockPanel.Dock="Bottom">  
  10. <StatusBarItem>This StatusBar works</StatusBarItem>  
  11. </StatusBar>  
  12. <Grid Height="300">  
  13. <Label Content="Test Label" Height="30" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="290" FontSize="14" BorderThickness="2" HorizontalContentAlignment="Center" BorderBrush="#7F000000" FontWeight="Bold" />  
  14. <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="12,63,0,0" VerticalAlignment="Top" Width="75" />  
  15. <CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left" Margin="115,63,0,0" VerticalAlignment="Top" />  
  16. <ListBox Height="100" HorizontalAlignment="Left" Margin="12,138,0,0" VerticalAlignment="Top" Width="75" DataContext="{Binding}">  
  17. <ListBoxItem Content="One" />  
  18. <ListBoxItem Content="Two" />  
  19. <ListBoxItem Content="Three" />  
  20. </ListBox>  
  21. <Grid.ContextMenu>  
  22. <ContextMenu>  
  23. <MenuItem Header="Cut"/>  
  24. <MenuItem Header="Copy"/>  
  25. <MenuItem Header="Paste"/>  
  26. </ContextMenu>  
  27. </Grid.ContextMenu>  
  28. </Grid>  
  29. </DockPanel>  

Using that we can get:

Note that if you put the following attributes in the Page tag then they will specify the size of the window within the IE window:

Height="350" Width="320"