How can I refresh a combobox after modifying its ItemsSource ObservableCollection

30,787

Solution 1

I think I have seen this before and the solution was to update the collection property to raise the change.

i.e.

public class MyViewModel : INotifyPropertyChanged
{
    private ObservableCollection<XmlNode> leadTypeCollection;

    public string LeadTypeCollection
    { 
        get { return leadTypeCollection; }
        set
        {
            if (value != leadTypeCollection)
            {
                leadTypeCollection = value;
                NotifyPropertyChanged("LeadTypeCollection");
            }
        }

    public MyViewModel()
    {
        leadTypeCollection = new ObservableCollection<XmlNode>();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        PropertyChanged.Raise(this, info);
    }
}

I have an extension method for raising the property (as found elsewhere on stackoverflow):

public static void Raise(this PropertyChangedEventHandler handler, object sender, string propertyName)
{
    if (null != handler)
    {
        handler(sender, new PropertyChangedEventArgs(propertyName));
    }
}

Solution 2

Firedragons answer would work, but i would prefer to initialize the LeadTypeCollection just once and use clear, add remove to update your collection.

var update = GetLeadTypesDataSource();     
this.LeadTypeCollection.Clear();

foreach(var item in update)
{
   this.LeadTypeCollection.Add(item);
}

your xaml binding should work if the datacontext is right

<ComboBox Name="LeadTypeComboBox" ItemsSource="{Binding LeadTypeCollection}" />

Solution 3

A simple method is to change ItemsSource with empty list and then change it back to your updated source. A snippet from my project which is working:

        RulesTable.ItemsSource = Rules.rulesEmpty;
        RulesTable.ItemsSource = Rules.Get();
Share:
30,787
Ruslan
Author by

Ruslan

Updated on July 15, 2022

Comments

  • Ruslan
    Ruslan almost 2 years

    The problems is simple: when ItemsSource is updated Combobox doesn't "refresh" e.g. new items don't appear to be added to the list of items in the combobox.

    I've tried the solution from aceepted answer to this question: WPF - Auto refresh combobox content with no luck.

    here's my code, XAML:

    <ComboBox Name="LeadTypeComboBox" ItemsSource="{Binding LeadTypeCollection}" />
    

    ViewModel:

    public ObservableCollection<XmlNode> LeadTypeCollection { get; set; }
    

    the way I update this collection is in the separate method, which loads data from updated XML file: this.LeadTypeCollection = GetLeadTypesDataSource();

    I've also tried using Add for testing purposes:

    this.LeadTypeCollection = GetLeadTypesDataSource();
    ItemToAdd = LeadTypeCollection[LeadTypeCollection.Count - 1];
    this.LeadTypeCollection.Add(ItemToAdd);
    

    the code updating collection definitely kicks off, I can see new items in this collection when debugging, but I don't see them in the combobox.

    Doing this in the xaml code-behind works: LeadTypeComboBox.ItemsSource = MyViewModel.GetLeadTypesDataSource(); but I'd like to achieve this with MVVM, i.e. the code must be in ViewModel which isn't aware of LeadTypeComboBox control.