Introduction to Binding ComboBox and DataGrid Controls in Silverlight 2.0 - Binding Object Collections Silverlight 2.0 ComboBox control dynamically
(Page 3 of 6 )
Now we have to develop a Silverlight 2.0 page which can consume our previously developed WCF Service. In this section, I will simply develop a Silverlight 2.0 page which has only a ComboBox control on it, and finally bind it to WCF object collection dynamically.
The following are the steps necessary to develop the above Silverlight 2.0 page:
Add a new “Silverlight” project (“DemoSL”) to the solution. This in turn will add a “DemoSL.Web” project (Silverlight hoster application) to the solution. Detailed steps are provided in previous articles.
Add a new “Silverlight user control” (“Page6.xaml”) to the “DemoSL” project and modify “Application_Startup” in “App.xaml” as follows:
Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
Me.RootVisual = New Page6()
End Sub
<UserControl x:Class="DemoSL.Page6"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<StackPanel x:Name="LayoutRoot" Background="White">
<StackPanel Height="36" Width="301" Orientation="Horizontal" HorizontalAlignment="Left" Margin="10,0,0,0">
<TextBlock Height="16" x:Name="lblDept" Width="100" Text="Select Dept:" TextWrapping="Wrap"/>
<ComboBox Height="20" x:Name="cboDept" Width="184"/>
</StackPanel>
<TextBlock Height="26" HorizontalAlignment="Left" x:Name="lblMsg" Width="350" Text="" TextWrapping="Wrap" Margin="10,0,0,0"/>
</StackPanel>
</UserControl>
The code behind the above markup is as follows:
Partial Public Class Page6
Inherits UserControl
Dim objService As New EmpService.EmpServiceClient
Public Sub New
InitializeComponent()
InitiateHandlers()
End Sub
Private Sub InitiateHandlers()
AddHandler objService.GetDepartmentListCompleted, AddressOf DeptListFetched
End Sub
Private Sub Page6_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
objService.GetDepartmentListAsync()
End Sub
Private Sub DeptListFetched(ByVal sender As Object, ByVal e As EmpService.GetDepartmentListCompletedEventArgs)
Me.cboDept.DisplayMemberPath = "Dname"
Me.cboDept.ItemsSource = e.Result
End Sub
Private Sub cboDept_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles cboDept.SelectionChanged
Me.lblMsg.Text = "You selected: " & CType(Me.cboDept.SelectedItem, EmpService.Dept).Deptno
End Sub
End Class
Explanation for the code is available in the last section.
It should give the output as follows:

If you could not execute your application (or if it is throwing any errors), please follow my previous articles, which discuss troubleshooting.
Next: Binding Object Collections to cascading Silverlight 2.0 ComboBoxes >>
More BrainDump Articles
More By Jagadish Chaterjee