Create a class that implements the IDictionary, INotifyCollectionChanged & INotifyPropertyChanged interfaces. The class would have an instance of Dictionary that it would use for the implementation of IDictionary (one of the Add methods is coded below as an example). Both INotifyCollectionChanged and INotifyProperyChanged require the presence of events, these events should be fired at appropriate points in the wrapper functions (again, consult the Add method below for an example)
class ObservableDictionary<TKey, TValue> : IDictionary, INotifyCollectionChanged, INotifyPropertyChanged
{
private Dictionary<TKey, TValue> mDictionary;
// Methods & Properties for IDictionary implementation would defer to mDictionary:
public void Add(TKey key, TValue value){
mDictionary.Add(key, value);
OnCollectionChanged(NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, value)
return;
}
// Implementation of INotifyCollectionChanged:
public event NotifyCollectionChangedEventHandler CollectionChanged;
protected void OnCollectionChanged(NotifyCollectionChangedEventArgs args){
// event fire implementation
}
// Implementation of INotifyProperyChanged:
public event ProperyChangedEventHandler ProperyChanged;
protected void OnPropertyChanged(PropertyChangedEventArgs args){
// event fire implementation
}
}
Edit:
Note that an implementation of the IDictionary interface directly or indirectly would require that three additional interfaces be implemented:
ICollection<KeyValuePair<TKey,TValue>>
IEnumerable<KeyValuePair<TKey,TValue>>
IEnumerable.
Depending on your needs you may not have to implement the entire IDictionary interface, if you are only going to be calling a couple of the methods then just implement those methods and the IDictionary interface becomes a luxury. You must implement the INotifyCollectionChanged and INotifyPropertyChanged interfaces for binding to work however.Blockquote