Wpf combobox itemssource not updating

The problem is simple: By default WPF compares Selected Item to each item in the Items Source by reference, meaning that unless the Selected Item points to the same item in memory as the Items Source item, it will decide that the item doesn’t exist in the Items Source and so no item gets selected.

wpf combobox itemssource not updating-19

If we don't open the drop down first (skip steps 2 and 4), then all items will appear correctly in the drop down.

Yeah, you need to open it to check the items, so after that all new items will not be added immediately.

It's called Observable Collection, and you use it much like a regular List - that's all it takes!

This will make the Add and Delete button work, but it won't do anything for the "Change name" button, because the change will happen on the bound data object itself and not the source list - the second step will handle that scenario though.

As you will learn in this article, you need just a bit of extra work for this to happen, but fortunately, WPF makes this pretty easy.

There are two different scenarios that you may or may not want to handle when dealing with data source changes: Changes to the list of items and changes in the bound properties in each of the data objects.

This is a bit more cumbersome than just changing the list type, like we did above, but it's still one of the simplest way to accomplish these automatic updates.

With the two changes described above, we now have an example that WILL reflect changes in the data source.

It looks like this: As you can see, implementing INotify Property Changed is pretty easy, but it does create a bit of extra code on your classes, and adds a bit of extra logic to your properties.

Tags: , ,