我有 2 个组合框,第一个的选定项目需要更改第二个的 itemsource 而它不需要。
这是我的xml...
<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);
}
我的两个来源是Dictionary<int,string>
...
如果我不单击(单击而不更改所选项目,只需单击)组合框,源列表就可以了。我点击它的那一刻,列表根本没有改变..