Pregunta

I have 2 comboboxes that the selected item of the first needs to change the itemsource of the second and it doesn't.

Here is my 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...

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

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

¿Fue útil?

Solución

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.

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.

<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}"/>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top