0
votes

I'm not understanding why my combo box doesn't update when I make a selection in another combobox. I'm still new to MVVM but in theory my code should work. I can populate the combos when the form loads but i need to refresh the combo with new values, and that's not working. I can see it retrieving new values but it never displays them on the form.

My XAML looks like this:

<ComboBox Grid.Column="1" ItemsSource="{Binding Path=Vendors}" SelectedItem="{Binding SelectedVendor, Mode=TwoWay}"   HorizontalAlignment="Left" Margin="24,12,0,11" Grid.Row="3" VerticalAlignment="Center" Width="293" />
<ComboBox x:Name="VendorProductServiceCB" HorizontalAlignment="Left" Margin="20.6,16.2,0,55.4" VerticalAlignment="Center" Width="293" Grid.Row="7" Grid.Column="1" ItemsSource="{Binding Path=VendorProductServices, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name" SelectedItem="{Binding Path=SelectedVendorProductService, Mode=TwoWay}" Height="22"/>

My ViewModel code is this:

public ObservableCollection<string> Vendors { get; set; } 
public ObservableCollection<VendorProductService> VendorProductServices { get; private set; }

public VendorProductService SelectedVendorProductService
    {
        get { return _selectedVendorProductService; }
        set { SetProperty(ref _selectedVendorProductService, value); }
    }

public string SelectedVendor
    {
        get { return _selectedVendor; }
        set
        {
            SetProperty(ref _selectedVendor, value);            
            SelectionChangedCommand.Execute(this);

        }               
    }

public FormSelectionViewModel()
    {
        Vendors = new ObservableCollection<string>(FetchVendors());

        VendorProductServices = new ObservableCollection<VendorProductService>(FetchVendorProductServices(_selectedVendor));  

        SelectionChangedCommand = new DelegateCommand(SelectionChanged);
    }
public  void SelectionChanged()
    {

        VendorProductServices = new ObservableCollection<VendorProductService>(FetchVendorProductServices(_selectedVendor)); 

    }
1

1 Answers

0
votes

You're not raising the PropertyChanged event for the VendorProductServices, because it's an auto property.

Either change it to:

private ObservableCollection<VendorProductService> _vendorProductServices;
public ObservableCollection<VendorProductService> VendorProductServices 
{ 
    get { return _vendorProductServices; }
    private set { SetProperty(ref _vendorProductServices, value); }
}        

Or change your collection properties to read only and use .Clear() and .Add() instead of creating new collections.