A problem you often meet when using MVVM is to get the selected items of an items control, especially Listbox.
You can easily bind the selected item or the current items but when multi selection comes in the way, it becomes harder because the SelectedItems (with an 's' property is not available to binding).
In this article, we will discover an easy way to bind yourself with an attached property to the SelectedItems property of the ListBox control.
We will use the Ramora pattern discussed before to bind ourself to the
selectionChanged events and then update the target list when the selection change.
If the target list implements
INotifyCollectionChanged, we will update the
listbox selection when this event is raised. Here is the resulting code:
public class ListBoxSelectedItemsSyncher : DependencyObject
private static List<Syncher> _synchers = new List<Syncher>();
Here we are.
Edit: I found out later another version of the same feature made by Samuel Jack on his blog: http://blog.functionalfun.net/2009/02/how-to-databind-to-selecteditems.html !