How to update a combo box after changing its ItemsSource ObservableCollection

The problems are simple: when updating ItemsSource Combobox is not updated, for example. new items do not appear in the list of items in the list.

I tried the solution from an aseptic answer to this question: WPF - update combobox contents to update with no luck.

here is my xaml code:

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

ViewModel:

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

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

I also tried using Add for testing:

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

The code update build is definitely starting, I see new items in this collection when debugging, but I don’t see them in the drop-down list.

Doing this in the work of LeadTypeComboBox.ItemsSource = MyViewModel.GetLeadTypesDataSource(); code works: LeadTypeComboBox.ItemsSource = MyViewModel.GetLeadTypesDataSource(); , but I would like to achieve this using MVVM, that is, the code should be in the ViewModel, which does not know about managing the LeadTypeComboBox.

+6
source share
3 answers

I think I have seen this before, and the solution was to update the collection property to raise this 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 to raise a property (as found elsewhere in stackoverflow):

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

The answer to Firedragons will work, but I would prefer to initialize the LeadTypeCollection only 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}" /> 
+6
source

A simple method is to change the ItemsSource with an empty list, and then change it back to your updated source. Excerpt from my project that works:

  RulesTable.ItemsSource = Rules.rulesEmpty; RulesTable.ItemsSource = Rules.Get(); 
0
source

Source: https://habr.com/ru/post/900191/


All Articles