WPF,MVVM使用另一个组合框选择的项目填充组合框

时间:2013-06-03 11:38:43

标签: wpf data-binding mvvm combobox population

我有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> ...

如果我没有点击(点击没有更改所选项目,只需点击)组合框,源列表就可以了。我点击它的那一刻,列表根本没有改变..

1 个答案:

答案 0 :(得分:3)

问题是,您绑定了Dictionary<int, string>,它没有一种机制来通知视图有关更改。视图无法识别,它必须更新。

所以我想,如果你想改变你的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}"/>