基于Silverlight数据网格内另一个组合框的SelectedItem更新组合框项资源
我在一个基于Silverlight数据网格内另一个组合框的SelectedItem更新组合框项资源,silverlight,dynamic,datagrid,combobox,selecteditem,Silverlight,Dynamic,Datagrid,Combobox,Selecteditem,我在一个数据网格中有两个组合框。当在第一个组合框中进行选择时,我想更新第二个组合框的项目资源。我刚刚开始学习Silverlight,我正在尝试遵循MVVM模式。这就是我到目前为止得到的 型号 public class Country : ViewModelBase { private string name; public string Name {...} public string Code { get; set; } } public class City : Vi
数据网格中有两个组合框
。当在第一个组合框
中进行选择时,我想更新第二个组合框
的项目资源
。我刚刚开始学习Silverlight
,我正在尝试遵循MVVM
模式。这就是我到目前为止得到的
型号
public class Country : ViewModelBase
{
private string name;
public string Name {...}
public string Code { get; set; }
}
public class City : ViewModelBase
{
private string name;
public string Name {...}
public string Code { get; set; }
}
public class Location : ViewModelBase
{
private City city;
public City City {...}
private Country country;
public Country Country {...}
}
public class CountryCityViewModel : ViewModelBase
{
private Location selectedLocation;
public Location SelectedLocation {...}
IRepo Repo { get; set; }
public ObservableCollection<Location> Locations { get; set; }
public ObservableCollection<Country> Countries { get; set; }
public ObservableCollection<City> Cities { get; set; }
public CountryCityViewModel(IRepo repo)
{
this.Repo = repo;
Countries = new ObservableCollection<Country>(repo.LoadCountries());
Cities = new ObservableCollection<City>();
Locations = new ObservableCollection<Location>(repo.LoadLocations());
this.PropertyChanged += CountryCityViewModel_PropertyChanged;
}
private void UpdateCityComboBoxItemsSource(object obj)
{
Country selCountry = ( (Country) obj );
if (obj != null)
{
UpdateCities(selCountry);
}
}
void CountryCityViewModel_PropertyChanged(object sender,
System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "SelectedLocation")
{
UpdateCities(SelectedLocation.Country);
}
}
private void UpdateCities(Country country)
{
var newCities = Repo.LoadCities(country);
Cities.Clear();
foreach (var city in newCities)
{
Cities.Add(city);
}
OnPropertyChanged("Cities");
}
}
视图模型
public class Country : ViewModelBase
{
private string name;
public string Name {...}
public string Code { get; set; }
}
public class City : ViewModelBase
{
private string name;
public string Name {...}
public string Code { get; set; }
}
public class Location : ViewModelBase
{
private City city;
public City City {...}
private Country country;
public Country Country {...}
}
public class CountryCityViewModel : ViewModelBase
{
private Location selectedLocation;
public Location SelectedLocation {...}
IRepo Repo { get; set; }
public ObservableCollection<Location> Locations { get; set; }
public ObservableCollection<Country> Countries { get; set; }
public ObservableCollection<City> Cities { get; set; }
public CountryCityViewModel(IRepo repo)
{
this.Repo = repo;
Countries = new ObservableCollection<Country>(repo.LoadCountries());
Cities = new ObservableCollection<City>();
Locations = new ObservableCollection<Location>(repo.LoadLocations());
this.PropertyChanged += CountryCityViewModel_PropertyChanged;
}
private void UpdateCityComboBoxItemsSource(object obj)
{
Country selCountry = ( (Country) obj );
if (obj != null)
{
UpdateCities(selCountry);
}
}
void CountryCityViewModel_PropertyChanged(object sender,
System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "SelectedLocation")
{
UpdateCities(SelectedLocation.Country);
}
}
private void UpdateCities(Country country)
{
var newCities = Repo.LoadCities(country);
Cities.Clear();
foreach (var city in newCities)
{
Cities.Add(city);
}
OnPropertyChanged("Cities");
}
}
和xaml
<ComboBox x:Name="CountryComboBox"
ItemsSource="{Binding Path=DataContext.Countries,
RelativeSource={RelativeSource AncestorType=Custom:CustomDataGrid}}"
SelectedItem="{Binding Path=Country, Mode=TwoWay}"
DisplayMemberPath="Name">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding
Path=DataContext.CountryComboBoxItemSelectedCommand,
RelativeSource={RelativeSource AncestorType=Custom:CustomDataGrid}}"
CommandParameter="{Binding SelectedItem,ElementName=CountryComboBox}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
我已经做过很多次了
我以前做的是在XAML中为Country组合框创建SelectedItem属性
假设SelectedItem属性的名称为“SelectedCountry”。现在转到该属性的Setter,并将Itemsource分配给MVVM中的城市组合框
private Country _SelectedCountry;
public Country SelectedCountry
{
get
{
return _SelectedCountry;
}
set
{
if (_SelectedCountry!= value)
{
_SelectedCountry= value;
AllCities = get this from your datasource.
RaisePropertyChanged("SelectedCountry");
}
}
}
希望这对你有帮助
快乐学习 ItemsSource应该如何更改?换成什么?我只能在你的代码中看到一个城市代码列表,那么你想实现什么呢?再次将该列表换成完全相同的列表?或者您想更改Address对象中的城市代码以匹配城市名称吗?@Martin我的问题可能没有最好的例子。我更新了一个更好的问题。我要寻找的是,当用户从Country组合框
中选择Country
时,我想用与所选国家相关的城市
对象集合更新城市组合框
的可用选项。