我有2个组合框,第一个的选定项需要更改第二个的项源,而没有。
这是我的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}"/>
这是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);
}
我的2个来源是
Dictionary<int,string>
...如果我不单击(单击而不更改所选项目,只需单击)组合框,则源列表很好。我单击它的那一刻,列表完全没有改变。
最佳答案
很好的问题是,您绑定(bind)了Dictionary<int, string>
,它没有机制将更改通知给 View 。该 View 无法识别,它必须进行更新。
因此,我想,如果您要像这样更改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}"/>
关于wpf - WPF,MVVM使用另一个组合框选择项填充组合框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16895852/