3.1. XAML Basics
<UserControl x:Class="SilverlightApplication1.MainPage" xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:d=http://schemas.microsoft.com/expression/blend/2008 xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"> <Grid x:Name="LayoutRoot"> </Grid> </UserControl>
2. XAML Namespaces
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
3. Design Namespaces
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
4.Custom Namespaces
xmlns:w="clr-namespace:Widgets"
<w:HotButton Text="Click Me!" Click="DoSomething"></w:HotButton>
5. Properties and Events in XAML
<Grid x:Name="grid1"> <Grid.Background> <LinearGradientBrush> <LinearGradientBrush.GradientStops> <GradientStop Offset="0.00" Color="Yellow" /> <GradientStop Offset="0.50" Color="White" /> <GradientStop Offset="1.00" Color="Purple" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Grid.Background> ... </Grid>
-----------------------------------------------------------------------
LinearGradientBrush brush = new LinearGradientBrush(); GradientStop gradientStop1 = new GradientStop(); gradientStop1.Offset = 0; gradientStop1.Color = Colors.Yellow; brush.GradientStops.Add(gradientStop1); GradientStop gradientStop2 = new GradientStop(); gradientStop2.Offset = 0.5; gradientStop2.Color = Colors.White; brush.GradientStops.Add(gradientStop2); GradientStop gradientStop3 = new GradientStop(); gradientStop3.Offset = 1; gradientStop3.Color = Colors.Purple; brush.GradientStops.Add(gradientStop3); grid1.Background = brush;
6. Nesting Elements
<Grid x:Name="grid1"> ... <TextBox x:Name="txtQuestion" ... > </TextBox> <Button x:Name="cmdAnswer" ... > </Button> <TextBox x:Name="txtAnswer" ... > </TextBox> </Grid>
-----------------------------------------------
txtQuestion = new TextBox(); ... grid1.Children.Add(txtQuestion); cmdAnswer = new Button(); ... grid1.Children.Add(cmdAnswer); txtAnswer = new TextBox(); ... grid1.Children.Add(txtAnswer);
------------------------------------------------------
private void Clear(DependencyObject element) { // If this is a text box, clear the text. TextBox txt = element as TextBox; if (txt != null) txt.Text = ""; // Check for nested children. int children = VisualTreeHelper.GetChildrenCount(element); for (int i = 0; i < children; i++) { DependencyObject child = VisualTreeHelper.GetChild(element, i); Clear(child); } }