My C# ObservableDictionary
by Joel Holder
namespace DynamicSpikes public event PropertyChangedEventHandler PropertyChanged = delegate { }; private void OnCollectionChanged(object sender, NotifyCollectionChangedAction action, object value) private void OnPropertyChanged(object sender, string propertyName) public new void Add(string key, object value) //OnPropertyChanged(this, "Values"); public new bool Remove(string key) //OnPropertyChanged(this, "Values"); public new object this[string key] }using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.Specialized;</p>
{
public class ObservableDictionary<String, Object> : Dictionary<string, object>, INotifyPropertyChanged, INotifyCollectionChanged
{
public event NotifyCollectionChangedEventHandler CollectionChanged = delegate { };
{
if (CollectionChanged != null)
{
CollectionChanged(sender, new NotifyCollectionChangedEventArgs(action, value));
}
}
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
{
base.Add(key, value);
OnCollectionChanged(this, NotifyCollectionChangedAction.Add, new KeyValuePair<string, object>(key, value));
//OnPropertyChanged(this, "Keys");
//OnPropertyChanged(this, "Count");
}
{
var kvp = base[key];
var result = base.Remove(key);
if (result)
{
OnCollectionChanged(this, NotifyCollectionChangedAction.Remove, kvp);
//OnPropertyChanged(this, "Keys");
//OnPropertyChanged(this, "Count");
}
return result;
}
{
get
{
return base[key];
}
set
{
base[key] = value;
PropertyChanged(this, new PropertyChangedEventArgs(key));
}
}
}
</code>