父組合框,綁定SelectedItem
到屬性上你的模型:
<ComboBox x:Name="cbCountriesList"
DataContext="{Binding CountriesCitiesList}"
IsSynchronizedWithCurrentItem="true"
ItemSource="{Binding}"
SelectedItem={Binding Path=SomePropertyOnModel} />
哪裏SomePropertyOnModel
是同一類型的國家列表中的項目的。
對於孩子的ComboBox,一切都應該是相同的:
<ComboBox x:Name="cbCitiesList" VirtualizingStackPanel.IsVirtualizing="True"
ItemsSource="{Binding CountriesCitiesList}"
IsSynchronizedWithCurrentItem ="true"
ItemSource="{Binding}" />
側面說明:你會注意到,我特意加入了的ItemsSource綁定到兩個組合框。
在該模型中,每當SomePropertyOnModel
設置的基礎上,值更新CountriesCitiesList
收到,即,:
private string _somePropertyOnModel;
public string SomePropertyOnModel
{
get { return _somePropertyOnModel; }
set
{
_somePropertyOnModel = value;
// call NotifyPropertyChanged
UpdateCountriesCitiesList();
}
}
private void UpdateCountriesCitiesList()
{
// set CountriesCitiesList based on the
// SomePropertyOnModel value
// CountriesCitiesList should be an ObservableCollection and the values
// should be cleared and then added.
CountriesCitiesList.Clear();
CountriesCitiesList.Add("Springfield");
}