I've been trying to see if there is an easy/clever way to implement binding to ListBox.SelectedItems. If you have tried yourself, you will know, that markup binding using BindingExtension will not work - the property doesn't support it. So you are left with wiring up a handler for SelectionChanged and trying that route. The closest I've gotten is this post:
http://alexshed.spaces.live.com/blog/cns!71C72270309CE838!149.entry
Update: the above mentioned blog is no longer available, that author's current blog is here and the closest I could find to the referenced blog post is this StackOverflow answer.
Which implements all the necessary C# in a handy attached property. But it implements the "binding" as a One-Way, Target to Source. I'd like Two-Way binding.
Any ideas?
I've found an elegant solution, and I've just found time to write a blog post about it.
What I did was to create an attached property, SynchronizedSelectedItems that you can set on the ListBox (or DataGrid in fact). You databind this to a collection, and then, with a bit of magic, the SelectedItems property on the ListBox and your collection are kept in sync. You can download all the code from my blog post.
The "magic" is a class which listens out for CollectionChanged events in either collection, and propagates the changes to the other.
I was looking for a solution for this, and the proposed seemed overly complex. So, here is a new two way binding solution that is limited to just an attached property, and uses weak event handling for watching changes in the defined dependency property. I didn't spend any time making this bulletproof, but it does work.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication2
{
public class ListBoxHelper
{
private static Dictionary<int, bool> SynchToDPInProcessDictionary = new Dictionary<int, bool>();
private static Dictionary<int, bool> SynchToLBInProcessDictionary = new Dictionary<int, bool>();
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.RegisterAttached("SelectedItems", typeof(IList), typeof(ListBoxHelper),
new FrameworkPropertyMetadata((IList)null,
new PropertyChangedCallback(OnSelectedItemsChanged)));
public static IList GetSelectedItems(DependencyObject d)
{
return (IList)d.GetValue(SelectedItemsProperty);
}
public static void SetSelectedItems(DependencyObject d, IList value)
{
d.SetValue(SelectedItemsProperty, value);
}
private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var listBox = d as ListBox;
if (listBox == null)
throw new InvalidOperationException("ListBoxHelper should only be used with ListBox or ListBox derived classes (like ListView).");
int hashcode = listBox.GetHashCode();
// Gets set on the initial binding.
if (!SynchToDPInProcessDictionary.ContainsKey(hashcode))
{
SynchToDPInProcessDictionary[hashcode] = false;
SynchToLBInProcessDictionary[hashcode] = false;
var observableCollection = GetSelectedItems(listBox) as INotifyCollectionChanged;
if (observableCollection != null)
{
// Create a weak CollectionChanged event handler on the SelectedItems property
// that synchronizes the collection back to the listbox.
CollectionChangedEventManager.AddHandler(observableCollection,
delegate(object sender, NotifyCollectionChangedEventArgs e2)
{
SyncToLBSelectedItems(GetSelectedItems(d), (ListBox)d);
});
}
}
SynchToDPSelectedItems(listBox);
listBox.SelectionChanged += delegate
{
SynchToDPSelectedItems(listBox);
};
}
private static void SynchToDPSelectedItems(ListBox listBox)
{
int hashcode = listBox.GetHashCode();
if (SynchToLBInProcessDictionary[hashcode]) return;
SynchToDPInProcessDictionary[hashcode] = true;
try
{
IList dpSelectedItems = GetSelectedItems(listBox);
dpSelectedItems.Clear();
if (listBox.SelectedItems != null)
{
foreach (var item in listBox.SelectedItems)
dpSelectedItems.Add(item);
}
}
finally
{
SynchToDPInProcessDictionary[hashcode] = false;
}
}
private static void SyncToLBSelectedItems(IList dpSelectedItems, ListBox listBox)
{
int hashcode = listBox.GetHashCode();
if (SynchToDPInProcessDictionary[hashcode]) return;
SynchToLBInProcessDictionary[hashcode] = true;
try
{
listBox.SelectedItems.Clear();
if (dpSelectedItems != null)
{
foreach (var item in dpSelectedItems)
listBox.SelectedItems.Add(item);
}
}
finally
{
SynchToLBInProcessDictionary[hashcode] = false;
}
}
}
}