我一直在努力查看是否有一种简单/巧妙的方法来实现与ListBox.SelectedItems的绑定。如果你已经尝试过,你会知道,使用BindingExtension的标记绑定将不起作用 - 该属性不支持它。因此,您需要连接SelectionChanged的处理程序并尝试该路由。我得到的最接近的是这篇文章:
<击> http://alexshed.spaces.live.com/blog/cns!71C72270309CE838!149.entry 击>
更新:上述博客已不再可用,作者当前的博客is here以及我在参考博客帖子中找到的最接近的博客是this StackOverflow answer。
在一个方便的附加属性中实现所有必需的C#。但它实现了“绑定”作为单向,目标来源。我想要双向绑定。
有什么想法吗?
答案 0 :(得分:41)
我找到了一个优雅的解决方案,我刚刚抽出时间写一个blog post about it。
我所做的是创建一个附加属性,可以在ListBox(或实际上是DataGrid)上设置的SynchronizedSelectedItems。您将此数据绑定到一个集合,然后,通过一些魔术,ListBox上的SelectedItems属性和您的集合保持同步。您可以从我的博客文章下载所有代码。
“magic”是一个侦听任一集合中的CollectionChanged事件的类,并将更改传播到另一个集合。
答案 1 :(得分:0)
我一直在为此找一个解决方案,而这个建议看起来过于复杂。因此,这是一个新的双向绑定解决方案,仅限于附加属性,并使用弱事件处理来监视已定义的依赖项属性中的更改。我没有花时间制作这种防弹,但确实有效。
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;
}
}
}
}