|
我编写了以下类,该类实现(或尝试!)带有通知的字典:
public partial class ObservableDictionary : Dictionary, INotifyCollectionChanged
{
public ObservableDictionary() : base() { }
public ObservableDictionary(int capacity) : base(capacity) { }
public ObservableDictionary(IEqualityComparer comparer) : base(comparer) { }
public ObservableDictionary(IDictionary dictionary) : base(dictionary) { }
public ObservableDictionary(int capacity, IEqualityComparer comparer) : base(capacity, comparer) { }
public ObservableDictionary(IDictionary dictionary, IEqualityComparer comparer) : base(dictionary, comparer) { }
public event NotifyCollectionChangedEventHandler CollectionChanged;
public new TValue this[TKey key]
{
get
{
return base[key];
}
set
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, key, 0));
base[key] = value;
}
}
public new void Add(TKey key, TValue value)
{
base.Add(key, value);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, key, 0));
}
public new bool Remove(TKey key)
{
bool x = base.Remove(key);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, key, 0));
return x;
}
public new void Clear()
{
base.Clear();
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (CollectionChanged != null)
{
CollectionChanged(this, e);
}
}
}
在另一堂课中,我有一个
MyObservableDictionary.CollectionChanged
事件的侦听器:
我遇到的问题是该事件不会触发。我怎样才能解决这个问题?
还木有评论哦,快来抢沙发吧~