How can I refresh a combobox after modifying its ItemsSource ObservableCollection

蹲街弑〆低调 提交于 2019-12-07 01:25:00

问题


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.


回答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));
    }
}



回答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}" />



回答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();


来源:https://stackoverflow.com/questions/7914549/how-can-i-refresh-a-combobox-after-modifying-its-itemssource-observablecollecti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!