WPF - 我如何使用一个键(如字典)实现ObservableCollection <K,T>?
我已经使用WPF的ObservableCollection进行绑定,并且工作得很好。我现在真正想要的是像一个字典,它有一个我可以使用的关键字,像“ObservableCollection”一样有效。WPF - 我如何使用一个键(如字典)实现ObservableCollection <K,T>?
你能提出可以用来提供这样一个ObservableCollection的代码吗?我们的目标是让我可以从WPF绑定一个类似于结构的词典。
创建一个类实现IDictionary,INotifyCollectionChanged & INotifyPropertyChanged的接口。该类将有一个Dictionary实例用于实现IDictionary(其中一个Add方法编码如下)。无论INotifyCollectionChanged和INotifyProperyChanged需要事件的情况下,这些事件应该在适当的点在包装功能解雇(再次,咨询为例添加下面的方法)
class ObservableDictionary<TKey, TValue> : IDictionary, INotifyCollectionChanged, INotifyPropertyChanged
{
private Dictionary<TKey, TValue> mDictionary;
// Methods & Properties for IDictionary implementation would defer to mDictionary:
public void Add(TKey key, TValue value){
mDictionary.Add(key, value);
OnCollectionChanged(NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, value)
return;
}
// Implementation of INotifyCollectionChanged:
public event NotifyCollectionChangedEventHandler CollectionChanged;
protected void OnCollectionChanged(NotifyCollectionChangedEventArgs args){
// event fire implementation
}
// Implementation of INotifyProperyChanged:
public event ProperyChangedEventHandler ProperyChanged;
protected void OnPropertyChanged(PropertyChangedEventArgs args){
// event fire implementation
}
}
编辑:
注意的实现IDictionary接口的直接或间接地将需要三个额外的接口来实现:
ICollection<KeyValuePair<TKey,TValue>>
IEnumerable<KeyValuePair<TKey,TValue>>
IEnumerable.
根据您的需求,你可能没有实现整个的IDictionary INTE如果你只打算调用几个方法,那么只需实现这些方法,IDictionary接口就变成了一种奢侈。您必须实现INotifyCollectionChanged和INotifyPropertyChanged接口才能进行绑定.Blockquote
把它放到VS我注意到有更多的接口方法需要实现 - 这是正确的吗? – Greg 2010-09-29 02:58:17
+0
是的,如果您的要求要求您实施整个IDictionary界面,请参阅我的编辑 – 2010-09-29 03:28:45
0
什么是这样的:
ObservableCollection<Tuple<string, object>>()
其中字符串对象和样品类型当然
根据使用情况,这可能有效。但是,考虑如何在使用这样的元组时通过键'找到'元素。字典可以使用散列查找按键的值,并且可以比搜索匹配键的列表快得多。 – MPavlak 2016-08-10 13:54:37
+0
您提到了一点。然而,使用可观察的集合来处理巨大的数据集可能不是一个好主意。 – eka808 2016-08-16 00:17:23
0
公共类ObservableDictonary:字典,INotifyCollectionChanged,INotifyPropertyChanged的 { 公共事件NotifyCollectionChangedEventHandler CollectionChanged;
public event PropertyChangedEventHandler PropertyChanged;
public new void Add(TKey key, TValue value)
{
base.Add(key, value);
if (!TryGetValue(key, out _)) return;
var index = Keys.Count;
OnPropertyChanged(nameof(Count));
OnPropertyChanged(nameof(Values));
OnCollectionChanged(NotifyCollectionChangedAction.Add, value, index);
}
public new void Remove(TKey key)
{
if (!TryGetValue(key, out var value)) return;
var index = IndexOf(Keys, key);
OnPropertyChanged(nameof(Count));
OnPropertyChanged(nameof(Values));
OnCollectionChanged(NotifyCollectionChangedAction.Remove, value, index);
base.Remove(key);
}
public new void Clear()
{
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChanged?.Invoke(this, e);
}
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
CollectionChanged?.Invoke(this, e);
}
private void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
private void OnCollectionChanged(NotifyCollectionChangedAction action, object item)
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item));
}
private void OnCollectionChanged(NotifyCollectionChangedAction action, object item, int index)
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item, index));
}
private int IndexOf(KeyCollection keys, TKey key)
{
var index = 0;
foreach (var k in keys)
{
if (Equals(k, key))
return index;
index++;
}
return -1;
}
}
我ovveride添加,删除和清除。您必须了解,如果您使用扩展方法或采用Dictonary参数的简单方法,则不会看到更改,因为在此情况下,添加或删除方法将使用Dictonary(而不是ObservableDictonary)。所以你必须直接使用方法(添加或删除)ObservableDictonary