gpt4 book ai didi

c# - C# 的 ObservableDictionary

转载 作者:太空狗 更新时间:2023-10-29 20:10:58 26 4
gpt4 key购买 nike

我正在尝试使用 ObservableDictionary 的以下实现:ObservableDictionary (C#) .

当我在将字典绑定(bind)到 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) 中抛出在以下方法中:

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 param似乎对应于KeyValuePair<TKey, TValue> oldItem .

如何KeyValuePair<TKey, TValue>超出范围,我应该怎么做才能使这项工作正常进行?

最佳答案

这是我最后所做的:

[Serializable]
public class ObservableKeyValuePair<TKey,TValue>:INotifyPropertyChanged
{
#region properties
private TKey key;
private TValue value;

public TKey Key
{
get { return key; }
set
{
key = value;
OnPropertyChanged("Key");
}
}

public TValue Value
{
get { return value; }
set
{
this.value = value;
OnPropertyChanged("Value");
}
}
#endregion

#region INotifyPropertyChanged Members

[field:NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(name));
}

#endregion
}

[Serializable]
public class ObservableDictionary<TKey,TValue>:ObservableCollection<ObservableKeyValuePair<TKey,TValue>>, IDictionary<TKey,TValue>
{

#region IDictionary<TKey,TValue> Members

public void Add(TKey key, TValue value)
{
if (ContainsKey(key))
{
throw new ArgumentException("The dictionary already contains the key");
}
base.Add(new ObservableKeyValuePair<TKey, TValue>() {Key = key, Value = value});
}

public bool ContainsKey(TKey key)
{
//var m=base.FirstOrDefault((i) => i.Key == key);
var r = ThisAsCollection().FirstOrDefault((i) => Equals(key, i.Key));

return !Equals(default(ObservableKeyValuePair<TKey, TValue>), r);
}

bool Equals<TKey>(TKey a, TKey b)
{
return EqualityComparer<TKey>.Default.Equals(a, b);
}

private ObservableCollection<ObservableKeyValuePair<TKey, TValue>> ThisAsCollection()
{
return this;
}

public ICollection<TKey> Keys
{
get { return (from i in ThisAsCollection() select i.Key).ToList(); }
}

public bool Remove(TKey key)
{
var remove = ThisAsCollection().Where(pair => Equals(key, pair.Key)).ToList();
foreach (var pair in remove)
{
ThisAsCollection().Remove(pair);
}
return remove.Count > 0;
}

public bool TryGetValue(TKey key, out TValue value)
{
value = default(TValue);
var r = GetKvpByTheKey(key);
if (!Equals(r, default(ObservableKeyValuePair<TKey, TValue>)))
{
return false;
}
value = r.Value;
return true;
}

private ObservableKeyValuePair<TKey, TValue> GetKvpByTheKey(TKey key)
{
return ThisAsCollection().FirstOrDefault((i) => i.Key.Equals(key));
}

public ICollection<TValue> Values
{
get { return (from i in ThisAsCollection() select i.Value).ToList(); }
}

public TValue this[TKey key]
{
get
{
TValue result;
if (!TryGetValue(key,out result))
{
throw new ArgumentException("Key not found");
}
return result;
}
set
{
if (ContainsKey(key))
{
GetKvpByTheKey(key).Value = value;
}
else
{
Add(key, value);
}
}
}

#endregion

#region ICollection<KeyValuePair<TKey,TValue>> Members

public void Add(KeyValuePair<TKey, TValue> item)
{
Add(item.Key, item.Value);
}

public bool Contains(KeyValuePair<TKey, TValue> item)
{
var r = GetKvpByTheKey(item.Key);
if (Equals(r, default(ObservableKeyValuePair<TKey, TValue>)))
{
return false;
}
return Equals(r.Value, item.Value);
}

public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
throw new NotImplementedException();
}

public bool IsReadOnly
{
get { return false; }
}

public bool Remove(KeyValuePair<TKey, TValue> item)
{
var r = GetKvpByTheKey(item.Key);
if (Equals(r, default(ObservableKeyValuePair<TKey, TValue>)))
{
return false;
}
if (!Equals(r.Value,item.Value))
{
return false ;
}
return ThisAsCollection().Remove(r);
}

#endregion

#region IEnumerable<KeyValuePair<TKey,TValue>> Members

public new IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return (from i in ThisAsCollection() select new KeyValuePair<TKey, TValue>(i.Key, i.Value)).ToList().GetEnumerator();
}

#endregion
}

这个实现对用户来说就像字典,对 WPF 来说就像 ObservableCollection

关于c# - C# 的 ObservableDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10616554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com