Wpf treeview binding not updating


10-Feb-2018 02:36

As no one gave me an answer, I was forced to use my gray-matter and invent a solution of my own.Since a similar question came up again recently on Stack Overflow, I thought I’d share what I came up with.

You should only need to change the declaration from Dictionary in your code.These controls know how to monitor the collection for changes. Dictionaries basically work by aggregating an internal collection of key-value pairs.For a detailed explanation of how binding to a collection works, see the MSDN documentation. People often start by trying to derive a class from Dictionary in a class that aggregates a dictionary. In order to fire the necessary collection change notifications, you must know the index of an entry when it changes.Pros and Cons The benefit to using an observable dictionary, of course, is that the dictionary can serve as the Items Source for a databound control and you can still access the dictionary in code the same way you access any other dictionary. There are certainly some limitations inherent in the very idea of making a dictionary observable. When you impose the behaviors of an observable collection on a dictionary so that the framework can bind to it, you add overhead.

Also, a dictionary exposes its Values and Keys collections through separate properties of the same name.

In my solution, I define an attached property that you attach to a List Box (or Data Grid, or anything that inherits from Multi Selector) and allows you to specify a collection (via data binding, of course) that you want to be kept in sync with the Selected Items collection of the target.