A group blog from members of the VB team
I am back with details on how to work with Panels, XAML, and Controls in Silverlight. In this walkthrough, we will see how to work with Panels, XAML, and Controls to create a Data-Driven Silverlight Interface.
During this walkthrough we will cover the following topics:
Before you begin, you need to download the offline kit from the Firestarter Labs to use the existing applications.
To work with Panels, XAML, and Controls follow these three simple steps:
Step 1: Create a Data-Driven Interface Step 2: Create a Form Entry to edit the Interface Details Step 3: Add a Menu using the StackPanel
In this step, we will see how to work with XAML, work with controls by adding a DataGrid, and work with the visual editor, known as the Cider editor, in Visual Studio. To create a Data-Driven Interface, follow these steps:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
</Grid>
<TextBlock Height="Auto" Width="Auto" Grid.Row="0">
Contact Details
</TextBlock>
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<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}}">
<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>
Now let’s create a form entry to edit the interface details. In this step, we will see how to create a new UserControl and create a data entry form. To create a new UserControl, follow these steps:
<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"/>
xmlns:my="clr-namespace:PanelsControlsLab"
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
Private Sub AddContactButton_Unchecked(ByVal sender As Object, ByVal e As RoutedEventArgs)
Me.AddContactPanel.Visibility = System.Windows.Visibility.Collapsed
End Class
End Namespace
<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>
Now let’s add a menu using the StackPanel. In the StackPanel, the contained elements can be organized either vertically or horizontally by changing the Orientation property appropriately. In this case, we are going to add two more menu items (edit contacts and help) along with the Add new contact button and align the StackPanel horizontally. To add the menu items, follow these steps:
<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"/>
In this walkthrough, you have learned how to create a Data-Driven Silverlight Interface using Panels, XAML, and Controls . You have now successfully added a data entry form to the existing project and created a menu using the StackPanel.
You can find the full source code for the application here.