WPF,MVVM 使用另一个组合框选定项目填充组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16895852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF, MVVM Populate combobox using another combobox selected item
提问by Shlomi.K
I have 2 comboboxes that the selected item of the first needs to change the itemsource of the second and it doesn't.
我有 2 个组合框,第一个的选定项目需要更改第二个的项目源,而它不需要。
Here is my xaml...
这是我的xaml...
<ComboBox Grid.Column="1" ItemsSource="{Binding StationsList}" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding Path=StationTarget, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<ComboBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding MonitorsList}" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding Path=MonitorTarget, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
And this is the relevant properties and functions of the viewModel...
而这就是viewModel的相关属性和功能...
public ObservableCollection<StationViewModel> Stations
{
get { return _stations; }
set
{
if (_stations == value)
return;
_stations = value;
SetStationList();
OnPropertyChanged("Stations");
}
}
public Dictionary<int,string> StationsList
{
get
{
return _stationsList;
}
set
{
if (_stationsList == null)
return;
_stationsList = value;
OnPropertyChanged("StationsList");
}
}
public ObservableCollection<MonitorViewModel> Monitors
{
get { return _monitors; }
set
{
if (_monitors == value)
return;
_monitors = value;
SetMonitorsList();
OnPropertyChanged("Monitors");
}
}
public Dictionary<int, string> MonitorsList
{
get { return _monitorsList; }
}
public int StationTarget
{
get
{
return _mc.StationTarget ;
}
set
{
if (_mc.StationTarget == value)
return;
_mc.StationTarget = value;
SetMonitorsList();
SetStatus();
OnPropertyChanged("StationTarget");
OnPropertyChanged("MonitorsList");
}
}
private void SetStationList()
{
_stationsList.Add(0,"OFF");
foreach (StationViewModel station in Stations)
_stationsList.Add( Convert.ToInt32(station.SerialCode),station.StationName);
}
private void SetMonitorsList()
{
_monitorsList.Clear();
_monitorsList.Add(0, "OFF");
_filterdMonitors.Clear();
_filterdMonitors = _monitors.Where(x => x.StationSerial == StationTarget).ToObservableCollection<MonitorViewModel>();
foreach (MonitorViewModel monitor in _filterdMonitors)
_monitorsList.Add(Convert.ToInt32(monitor.Channel), monitor.MonitorName);
}
my 2 sources are Dictionary<int,string>...
我的两个来源是Dictionary<int,string>...
if I don't click (click without change selected item, just click) the combobox, the source list is fine. the minute I click it, the list does not change at all..
如果我不单击(单击而不更改所选项目,只需单击)组合框,源列表就可以了。我点击它的那一刻,列表根本没有改变..
回答by DHN
Well the problem is, that you have bound the Dictionary<int, string>, which don't have a mechanism to notify the view about the changes. The view cannot be aware, that it has to update.
那么问题是,您绑定了Dictionary<int, string>,它没有通知视图有关更改的机制。视图无法意识到它必须更新。
So I guess, if you would change your XAML like this, it's probably going to work, if you change your SetMonitorsList()afterwards. It should modify the content of Monitors.
所以我想,如果你像这样改变你的 XAML,它可能会起作用,如果你SetMonitorsList()以后改变你的。它应该修改Monitors.
<ComboBox Grid.Column="1" ItemsSource="{Binding Stations}" DisplayMemberPath="Value"
SelectedValuePath="Key" SelectedValue="{Binding Path=StationTarget, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<ComboBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding Monitors}"
DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding Path=MonitorTarget, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

