Introduction to Binding ComboBox and DataGrid Controls in Silverlight 2.0 - Binding Object Collections to cascading Silverlight 2.0 ComboBoxes
(Page 4 of 6 )
In the previous section, we make object collection bind to a single ComboBox. However, there will be times where cascade-type binding for ComboBoxes is necessary. In simple words, we will bind the first ComboBox (say Dept ComboBox) to one collection. Once the user selects an item in the first ComboBox, the second ComboBox (say Emp ComboBox) must be bound to related items.
The following is the markup for designing Silverlight UI with two ComboBoxes:
<UserControl x:Class="DemoSL.Page7"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">
<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" Canvas.ZIndex="100"/>
</StackPanel>
<StackPanel Height="36" Width="301" Orientation="Horizontal" HorizontalAlignment="Left" Margin="10,0,0,0">
<TextBlock Height="16" x:Name="lblEmp" Width="100" Text="Select Employee:" TextWrapping="Wrap"/>
<ComboBox Height="20" x:Name="cboEmp" Width="184" Canvas.ZIndex="100"/>
</StackPanel>
<TextBlock Height="26" HorizontalAlignment="Left" x:Name="lblMsg" Width="350" Text="" TextWrapping="Wrap" Margin="10,0,0,0"/>
</StackPanel>
</UserControl>
The following is the code behind the above markup:
Partial Public Class Page7
Inherits UserControl
Dim objService As New EmpService.EmpServiceClient
Public Sub New()
InitializeComponent()
InitiateHandlers()
End Sub
Private Sub InitiateHandlers()
AddHandler objService.GetDepartmentListCompleted, AddressOf DeptListFetched
AddHandler objService.GetEmployeeListByDeptCompleted, AddressOf EmpListFetched
End Sub
Private Sub Page7_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 = "Fetching Employees.."
Dim oDept As EmpService.Dept = Me.cboDept.SelectedItem
objService.GetEmployeeListByDeptAsync(oDept.Deptno)
End Sub
Private Sub EmpListFetched(ByVal sender As Object, ByVal e As EmpService.GetEmployeeListByDeptCompletedEventArgs)
Me.cboEmp.DisplayMemberPath = "Ename"
Me.cboEmp.ItemsSource = e.Result
Me.lblMsg.Text = "Fetched!"
End Sub
End Class
The explanation for the code is available in the last section.
It should give the output as follows:

Next: Binding Object Collections to Silverlight 2.0 ComboBox cascading with DataGrid >>
More BrainDump Articles
More By Jagadish Chaterjee