问题描述
我正在尝试使用 ObservableDictionary 的以下实现:ObservableDictionary (C#).
I'm trying to use following implementation of the ObservableDictionary: ObservableDictionary (C#).
当我在将字典绑定到 DataGrid 时使用以下代码时:
When I'm using following code while binding the dictionary to a DataGrid:
ObserveableDictionary<string,string> dd=new ObserveableDictionary<string,string>();
....
dd["aa"]="bb";
....
dd["aa"]="cc";
在 dd["aa"]="cc";
我收到以下异常
Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index
这个异常是在CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, newItem, oldItem)
中抛出的,方法如下:
This exception is thrown in CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, newItem, oldItem)
in the following method:
private void OnCollectionChanged(NotifyCollectionChangedAction action, KeyValuePair<TKey, TValue> newItem, KeyValuePair<TKey, TValue> oldItem)
{
OnPropertyChanged();
if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, newItem, oldItem));
}
index
参数似乎对应于 KeyValuePair旧项目
.
KeyValuePair
怎么会超出范围,我应该怎么做才能使它起作用?
How can KeyValuePair<TKey, TValue>
be out of range, and what should I do to make this work?
推荐答案
类似的数据结构,绑定到Dictionary类型的集合
Similar data structure, to bind to Dictionary type collection
http://drwpf.com/blog/2007/09/16/can-i-bind-my-itemscontrol-to-a-dictionary/
它提供了一个新的数据结构 ObservableDictionary 并触发 PropertyChanged 基础字典有任何变化.
It provides a new Data structure ObservableDictionary and fires PropertyChanged in case of any change to underlying Dictionary.
这篇关于用于 c# 的 ObservableDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!