9

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.

Community
  • 1
  • 1
Ruslan
  • 9,927
  • 15
  • 55
  • 89

3 Answers3

10

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}" />
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • That's the way I tend to do it too. The ObservableCollections built in property raising works well. The situation I showed is where you change the entire list which I guess doesn't raise a change – Firedragon Oct 27 '11 at 11:18
  • 2
    np your solution works well :) but i like my approach more. and as far as you work with ICollectionView for sorting and filtering - its easier to do it my way. – blindmeis Oct 27 '11 at 11:56
7

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();
vinsa
  • 1,132
  • 12
  • 25
7

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));
    }
}
Firedragon
  • 3,685
  • 3
  • 35
  • 75