You can achieve this with some clever ElementBinding s.
Basic example:
Say we have a simple class:
public class Country { public string Name { get; set; } public IEnumerable<string> Regions { get; set; } }
Then we will have two ComboBox es: one for choosing a country and the other for choosing a region in this country. The second should be updated when the value of the first changes.
Ok, first we have to tell Silverlight how to display the Country . For complex scenarios, we could use a DataTemplate for this, but for this we only need the DisplayMemberPath property of the ComboBox class.
<ComboBox x:Name="cbCountries" DisplayMemberPath="Name"/>
So, we are creating a simple collection of these objects in the code behind:
var countries = new List<Country>() { new Country { Name = "USA", Regions = new List<string> { "Texas", "New York", "Florida", ... }, }, new Country { Name = "UK", Regions = new List<string> { "Scotland", "Wales", "England", ... }, }, ... };
I know that these are not all regions in the sample countries, but this is a Silverlight example, not a geographic lesson.
Now we need to set the ItemsSource from ComboBox to this collection.
cbCountries.ItemsSource = countries;
Both of them can be in the constructor in the code. Ok, back to XAML!
We will need another ComboBox and a way to report that it will dynamically retrieve its elements from another collection.
Binding its ItemsSource to another ComboBox is the most obvious way to achieve this.
<ComboBox x:Name="cbRegion" ItemsSource="{Binding ElementName=cbCountries, Path=SelectedItem.Regions}"/>
That should make the trick pretty simple.
If you are using MVVM:
You can bind to the ItemsSource first ComboBox from ViewModel . The rest remains the same.
To find out what values ββare selected for the ViewModel , use the two-way bindings in the SelectedItem property of both ComboBox es, and associate this with any property that you have for it in the ViewModel .
If your collection can change dynamically:
If the list of countries (or what you want to use for this) can change at run time, it is best to implement INotifyPropertyChanged for the Country class and for the regions, use ObservableCollection<> .
If this does not need to be changed at run time, there is no need to worry about it.