Estimated Time: 45 minutes
Working with XAML is very similar to working with HTML, the main advantage being that you don’t have to worry about the pesky cross browser issues. Like HTML, XAML is XML. There are nodes, which define controls and attributes which are properties of the control. This exercise shows how to work with XAML, how to work with controls by adding a DataGrid, and how to work with the visual editor, known as the Cider editor, in Visual Studio.
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
</Grid>
It’s important to note that you won’t place any tags specifying the beginnings and ends of rows inside the grid, itself. Instead, row assignment is specified as an attribute inside each control tag.
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Height="Auto" Width="Auto" Grid.Row="0">
Contact Details
</TextBlock>
</Grid>
Rows are indexed from the top of the panel to the bottom, beginning with zero.
Take a look at the XAML generated by Visual Studio for the DataGrid conrol. The ‘sdk:’ at the beginning of the tag denotes an xmlns(XML Namespace)declaration. At the top of the page, you can see where this declaration was included with the others. This is a necessary addition if you want to use the DataGrid, as it is part of the Silverlight Toolkit and not part of the native runtime.
d:DesignHeight="566" d:DesignWidth="947" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Height="Auto" Width="Auto" Grid.Row="0">
Contact Details
</TextBlock>
<sdk:DataGrid AutoGenerateColumns="True" Grid.Row="1" Height="Auto"
Width="Auto" HorizontalAlignment="Left" Margin="0,0,16,0"
Name="dataGrid1" VerticalAlignment="Top"/>
</Grid>
<UserControl.Resources>
<DataTemplate x:Key="ImageTemplate">
<StackPanel>
<Image Source="{Binding Image}" HorizontalAlignment="Left"
Height="64" Width="64"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource Contacts}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Height="Auto" Width="Auto" Grid.Row="0" FontSize="24"
HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="8,8,0,0">Contact Details</TextBlock>
<sdk:DataGrid AutoGenerateColumns="False" Grid.Row="1" Height="Auto"
Width="Auto" HorizontalAlignment="Left" Margin="0,16,0,0"
x:Name="ContactsDataGrid" VerticalAlignment="Top"
ItemsSource="{Binding Collection}">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
<sdk:DataGridTemplateColumn CellTemplate="{StaticResource
ImageTemplate}" Header="Image"/>
<sdk:DataGridTextColumn Binding="{Binding Address}"
Header="Address"/>
<sdk:DataGridTextColumn Binding="{Binding Email}" Header="Email"/>
<sdk:DataGridTextColumn Binding="{Binding PhoneNumber}"
Header="PhoneNumber"/>
<sdk:DataGridTextColumn Binding="{Binding WebSite}"
Header="WebSite"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Grid>
And here’s what it looks like :
Creating an UI for an entry form lends itself to the creation of a page. In ASP.NET you would create a WebForm. In a Silverlight project, a UserControl used to create a new page. In this exercise you will learn how to create a new UserControl and create a data entry form.
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,16,0,0" Name="textBox1" VerticalAlignment="Top" Width="348" Text="Name" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="12,46,0,0" Name="textBox2" VerticalAlignment="Top" Width="348" Text="Image Location"/> <TextBox Height="23" HorizontalAlignment="Left" Margin="12,74,0,0" Name="textBox3" VerticalAlignment="Top" Width="348" Text="Address"/> <TextBox Height="23" HorizontalAlignment="Left" Margin="12,104,0,0" Name="textBox4" VerticalAlignment="Top" Width="348" Text="Email"/> <TextBox Height="23" HorizontalAlignment="Left" Margin="10,134,0,0" Name="textBox5" VerticalAlignment="Top" Width="350" Text="Phone Number"/> <TextBox Height="23" HorizontalAlignment="Left" Margin="10,164,0,0" Name="textBox6" VerticalAlignment="Top" Width="350" Text="Website"/>
You must have a page other than EditContacts.xaml as your currently open document for EditContacts to appear in the toolbox.
xmlns:my="clr-namespace:PanelsControlsLab"
If you don’t see the ToggleButton in the Toolbox, add it by right clicking the Toolbox and select Choose Items from the context menu.
From the Choose Toolbox Items dialog box, Find and select the ToggleButton. Now the ToggleButton is added to the Toolbox, find it and click and add it to your UI.
In this window you can edit the visual properties of whatever control you have selected at the moment. For now, you want the default visibility set to Collapsed.
There’s a shortcut if you want to add an Event Handler. If you select “Events” in the properties window of the CheckBox control, you’ll see an event labeled Checked. Double-click that event to automatically generate the event handler for the CheckBox. Do the same once more for the UnChecked event.
namespace PanelsControlsLab
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void AddContactButton_Checked(object sender,
System.Windows.RoutedEventArgs e)
{
this.AddContactPanel.Visibility =
System.Windows.Visibility.Visible;
}
private void AddContactButton_Unchecked(object sender,
RoutedEventArgs e)
{
this.AddContactPanel.Visibility =System.Windows.Visibility.Collapsed;
}
}
}
Namespace PanelsControlsLab
Partial Public Class MainPage
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
Private Sub AddContactButton_Checked(ByVal sender As Object,ByVal e As System.Windows.RoutedEventArgs)
Me.AddContactPanel.Visibility = System.Windows.Visibility.Visible
End Sub
Private Sub AddContactButton_Unchecked(ByVal sender As Object, ByVal e As RoutedEventArgs)
Me.AddContactPanel.Visibility = System.Windows.Visibility.Collapsed
End Sub
End Class
End Namespace
(A simple border around the edit contacts window has been added, and set the Height and Width of editContacts1 in MainPage.Xaml to ‘Auto’. This was done entirely for aesthetic purposes, and is optional).
<Button Content="Add Contact" Height="23" HorizontalAlignment="Left" Margin="224,194,0,0" Name="button1" VerticalAlignment="Top" Width="136" />
<Border BorderThickness="1" Height="227" HorizontalAlignment="Left" Name="border1" VerticalAlignment="Top" Width="372">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="#FF443B3B" Offset="1" />
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
So far you’ve learned how to add controls, use the DataGrid, work with the Grid, and add event handlers. In this exercise you’ll take a look at another type of Panel, the StackPanel. The StackPanel is unique because it handles aligning the contained elements for you. By specifying the Orientation property. The can be organized either Vertically or Horizontally. In this case you are going to add two more menu items (edit contacts and help) next to the Add new contact button and align the StackPanel horizontally.
<StackPanel HorizontalAlignment="Right" Margin="0,8,8,11"
Orientation="Horizontal">
<ToggleButton x:Name="AddContactButton" Content="Add new contact" Checked="AddContactButton_Checked" Unchecked="AddContactButton_Unchecked" Height="24" Margin="0,0,5,0" />
<ToggleButton x:Name="EditContactButton" Content="Edit contact" Checked="AddContactButton_Checked" Unchecked="AddContactButton_Unchecked" Height="24" Margin="0,0,5,0" />
<Button x:Name="HelpButton" Content="Help"/>
</StackPanel>

In this exercise you learned how to create a data driven Silverlight interface from the ground up. You added a data entry form to the existing prorject and created a menu with the StackPanel. In this application you satisfied the following requirements:
This lab is the foundation for creating a data driven Silverlight interface. Other labs will provide additional details about application design practices such as Model-View-ViewModel (MVVM), how to use Expression Blend, how to integrate services, and how to create desktop applications with Silverlight.
Comments (0)