Wpf treeview observablecollection not updating

WPF is used for the demo, but this approach can be used in UWP, Xamarin, Windows Forms, and maybe some other technologies. Many moons ago, I asked on the WPF forums if anybody had a way of data-binding the Selected Items property of a List Box.This listens to Collection Changed events in both collections, and when either changes, it updates the other.To help me demonstrate, I’ve knocked up a toy View Model: it has on it an Available Names property that will be the data source for the List Box, a Selected Names property which is the reason we’re going through the whole exercise, and a Summary property which displays the number of selected items to prove that the data binding is working correctly.It is quite a common situation when UI shows a lot of different kinds of elements (text blocks, images, graphics, etc.) structured in different ways (lists, trees, etc.), but only one of these elements could be selected at the same time.In this article, I will try to create a class which will help to deal with selection.To work properly, the collection you give should implement INotify Collection Changed – using Observable Collection should do the trick.

Let's add a class View Model, with an Observable Collection of Persons, which we pre-populate at construction time. In the "startup" code of the application, we instantiate a View Model, and pass it as the Data Context to the View, in this case, the Window.With that in place the Selected Names collection in my View Model is updated whenever Selected Items on the List Box changes.And to prove it works the other way to, I’ve created a Select All command on the View Model that puts all available names into the Selected Names collection.When you press the button you’ll see that the List Box obediently updates to show all items selected.