I have written the following class which implements(or tries to!) a dictionary with notifications:
public partial class ObservableDictionary
Your solution - Fixed ;)
public class ObservableDictionary : Dictionary, INotifyCollectionChanged, INotifyPropertyChanged {
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 event PropertyChangedEventHandler PropertyChanged;
public new TValue this[TKey key] {
get {
return base[key];
}
set {
TValue oldValue;
bool exist = base.TryGetValue(key, out oldValue);
var oldItem = new KeyValuePair(key, oldValue);
base[key] = value;
var newItem = new KeyValuePair(key, value);
if (exist) {
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItem, oldItem, base.Keys.ToList( ).IndexOf(key)));
} else {
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newItem, base.Keys.ToList( ).IndexOf(key)));
this.OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
}
}
}
public new void Add(TKey key, TValue value) {
if (!base.ContainsKey(key)) {
var item = new KeyValuePair(key, value);
base.Add(key, value);
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, base.Keys.ToList( ).IndexOf(key)));
this.OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
}
}
public new bool Remove(TKey key) {
TValue value;
if (base.TryGetValue(key, out value)) {
var item = new KeyValuePair(key, base[key]);
bool result = base.Remove(key);
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, base.Keys.ToList( ).IndexOf(key)));
this.OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
return result;
}
return false;
}
public new void Clear( ) {
base.Clear( );
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
this.OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
}
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e) {
if (this.CollectionChanged != null) {
this.CollectionChanged(this, e);
}
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {
if (this.PropertyChanged != null) {
this.PropertyChanged(this, e);
}
}
}