<Application x:Class="SimpleXamlApp.MyApp" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml" Exit ="AppExit" > <x:Code> void AppExit(object sender, ExitEventArgs e) { MessageBox.Show("App has exited"); } </x:Code> </Application>
Windows Presentation Foundation
Inner element with xml namespace
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <TextBlock> Hello, World! </TextBlock> </StackPanel> </Page>
Markup Extensions with Property Elements.xaml
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" HorizontalAlignment="Center" VerticalAlignment="Center"> <Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Button.Background> <x:Null/> </Button.Background> <Button.Height> <x:Static Member="SystemParameters.IconHeight"/> </Button.Height> <Button.Content> <Binding Path="Height"> <Binding.RelativeSource> <RelativeSource Mode="Self"/> </Binding.RelativeSource> </Binding> </Button.Content> </Button> </Page>
Markup Extensions for Button
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" HorizontalAlignment="Center" VerticalAlignment="Center"> <Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="{x:Null}" Height="{x:Static SystemParameters.IconHeight}" Content="{Binding Path=Height, RelativeSource={RelativeSource Self}}"/> </Page>
Xaml Button with namespace
<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Foreground="LightSeaGreen" FontSize="24pt"> Hello, XAML! </Button>
Embedded Code in Window.xaml
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="EmbeddedCodeWindow" Title="Embed Code in XAML" SizeToContent="WidthAndHeight" ResizeMode="CanMinimize" Loaded="WindowOnLoaded"> <StackPanel> <Button HorizontalAlignment="Center" Margin="24" Click="ButtonOnClick"> Click the Button </Button> <Ellipse Name="elips" Width="200" Height="100" Margin="24" Stroke="Red" StrokeThickness="10" /> <ListBox Name="lstbox" Width="150" Height="150" Margin="24" SelectionChanged="ListBoxOnSelection" /> <x:Code> void WindowOnLoaded(object sender, RoutedEventArgs args) { foreach (System.Reflection.PropertyInfo prop in typeof(Brushes).GetProperties()) lstbox.Items.Add(prop.Name); } void ButtonOnClick(object sender, RoutedEventArgs args) { Button btn = sender as Button; MessageBox.Show("The button labeled '" + btn.Content + "' has been clicked."); } void ListBoxOnSelection(object sender, SelectionChangedEventArgs args) { string strItem = lstbox.SelectedItem as string; System.Reflection.PropertyInfo prop = typeof(Brushes).GetProperty(strItem); elips.Fill = (Brush)prop.GetValue(null, null); } </x:Code> </StackPanel> </Window>
Property Element Syntax
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ScrollBar Name="scroll" Orientation="Horizontal" Margin="24" Maximum="100" LargeChange="10" SmallChange="1" /> <Label HorizontalAlignment="Center"> <Label.Content> <Binding ElementName="scroll" Path="Value" /> </Label.Content> </Label> </StackPanel>