Databound listbox not updating
Databound listbox not updating - Teen dating site 13 29
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.
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.
The Two List Synchronizer code does not make especially thrilling reading, so I won’t show it here.
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.
Standard data binding doesn’t work, because the Selected Items property is read-only, and understandably so: how would you like it if I injected an arbitrary collection into your List Box and expected you to keep it up to date as the selection changed?
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.
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.
To work properly, the collection you give should implement INotify Collection Changed – using Observable Collection should do the trick.
When you set the property, I instantiate another class of my invention, a Two List Synchronizer.
This listens to Collection Changed events in both collections, and when either changes, it updates the other.