2

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

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

4

1 回答 1

4

那么问题是,您已经绑定了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}"/>
于 2013-06-03T12:01:23.780 回答