ObservableDictionary.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Linq;
  3. using System.ComponentModel;
  4. using System.Collections.Generic;
  5. using System.Collections;
  6. using specialized = PlatformSupport.Collections.Specialized;
  7. namespace PlatformSupport.Collections.ObjectModel
  8. {
  9. public class ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, specialized.INotifyCollectionChanged, INotifyPropertyChanged
  10. {
  11. private const string CountString = "Count";
  12. private const string IndexerName = "Item[]";
  13. private const string KeysName = "Keys";
  14. private const string ValuesName = "Values";
  15. private IDictionary<TKey, TValue> _Dictionary;
  16. protected IDictionary<TKey, TValue> Dictionary
  17. {
  18. get { return _Dictionary; }
  19. }
  20. #region Constructors
  21. public ObservableDictionary()
  22. {
  23. _Dictionary = new Dictionary<TKey, TValue>();
  24. }
  25. public ObservableDictionary(IDictionary<TKey, TValue> dictionary)
  26. {
  27. _Dictionary = new Dictionary<TKey, TValue>(dictionary);
  28. }
  29. public ObservableDictionary(IEqualityComparer<TKey> comparer)
  30. {
  31. _Dictionary = new Dictionary<TKey, TValue>(comparer);
  32. }
  33. public ObservableDictionary(int capacity)
  34. {
  35. _Dictionary = new Dictionary<TKey, TValue>(capacity);
  36. }
  37. public ObservableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)
  38. {
  39. _Dictionary = new Dictionary<TKey, TValue>(dictionary, comparer);
  40. }
  41. public ObservableDictionary(int capacity, IEqualityComparer<TKey> comparer)
  42. {
  43. _Dictionary = new Dictionary<TKey, TValue>(capacity, comparer);
  44. }
  45. #endregion
  46. #region IDictionary<TKey,TValue> Members
  47. public void Add(TKey key, TValue value)
  48. {
  49. Insert(key, value, true);
  50. }
  51. public bool ContainsKey(TKey key)
  52. {
  53. return Dictionary.ContainsKey(key);
  54. }
  55. public ICollection<TKey> Keys
  56. {
  57. get { return Dictionary.Keys; }
  58. }
  59. public bool Remove(TKey key)
  60. {
  61. if (key == null) throw new ArgumentNullException("key");
  62. TValue value;
  63. Dictionary.TryGetValue(key, out value);
  64. var removed = Dictionary.Remove(key);
  65. if (removed)
  66. //OnCollectionChanged(NotifyCollectionChangedAction.Remove, new KeyValuePair<TKey, TValue>(key, value));
  67. OnCollectionChanged();
  68. return removed;
  69. }
  70. public bool TryGetValue(TKey key, out TValue value)
  71. {
  72. return Dictionary.TryGetValue(key, out value);
  73. }
  74. public ICollection<TValue> Values
  75. {
  76. get { return Dictionary.Values; }
  77. }
  78. public TValue this[TKey key]
  79. {
  80. get
  81. {
  82. return Dictionary[key];
  83. }
  84. set
  85. {
  86. Insert(key, value, false);
  87. }
  88. }
  89. #endregion
  90. #region ICollection<KeyValuePair<TKey,TValue>> Members
  91. public void Add(KeyValuePair<TKey, TValue> item)
  92. {
  93. Insert(item.Key, item.Value, true);
  94. }
  95. public void Clear()
  96. {
  97. if (Dictionary.Count > 0)
  98. {
  99. Dictionary.Clear();
  100. OnCollectionChanged();
  101. }
  102. }
  103. public bool Contains(KeyValuePair<TKey, TValue> item)
  104. {
  105. return Dictionary.Contains(item);
  106. }
  107. public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  108. {
  109. Dictionary.CopyTo(array, arrayIndex);
  110. }
  111. public int Count
  112. {
  113. get { return Dictionary.Count; }
  114. }
  115. public bool IsReadOnly
  116. {
  117. get { return Dictionary.IsReadOnly; }
  118. }
  119. public bool Remove(KeyValuePair<TKey, TValue> item)
  120. {
  121. return Remove(item.Key);
  122. }
  123. #endregion
  124. #region IEnumerable<KeyValuePair<TKey,TValue>> Members
  125. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  126. {
  127. return Dictionary.GetEnumerator();
  128. }
  129. #endregion
  130. #region IEnumerable Members
  131. IEnumerator IEnumerable.GetEnumerator()
  132. {
  133. return ((IEnumerable)Dictionary).GetEnumerator();
  134. }
  135. #endregion
  136. #region INotifyCollectionChanged Members
  137. public event specialized.NotifyCollectionChangedEventHandler CollectionChanged;
  138. #endregion
  139. #region INotifyPropertyChanged Members
  140. public event PropertyChangedEventHandler PropertyChanged;
  141. #endregion
  142. public void AddRange(IDictionary<TKey, TValue> items)
  143. {
  144. if (items == null) throw new ArgumentNullException("items");
  145. if (items.Count > 0)
  146. {
  147. if (Dictionary.Count > 0)
  148. {
  149. if (items.Keys.Any((k) => Dictionary.ContainsKey(k)))
  150. throw new ArgumentException("An item with the same key has already been added.");
  151. else
  152. foreach (var item in items) Dictionary.Add(item);
  153. }
  154. else
  155. _Dictionary = new Dictionary<TKey, TValue>(items);
  156. OnCollectionChanged(specialized.NotifyCollectionChangedAction.Add, items.ToArray());
  157. }
  158. }
  159. private void Insert(TKey key, TValue value, bool add)
  160. {
  161. if (key == null) throw new ArgumentNullException("key");
  162. TValue item;
  163. if (Dictionary.TryGetValue(key, out item))
  164. {
  165. if (add) throw new ArgumentException("An item with the same key has already been added.");
  166. if (Equals(item, value)) return;
  167. Dictionary[key] = value;
  168. OnCollectionChanged(specialized.NotifyCollectionChangedAction.Replace, new KeyValuePair<TKey, TValue>(key, value), new KeyValuePair<TKey, TValue>(key, item));
  169. }
  170. else
  171. {
  172. Dictionary[key] = value;
  173. OnCollectionChanged(specialized.NotifyCollectionChangedAction.Add, new KeyValuePair<TKey, TValue>(key, value));
  174. }
  175. }
  176. private void OnPropertyChanged()
  177. {
  178. OnPropertyChanged(CountString);
  179. OnPropertyChanged(IndexerName);
  180. OnPropertyChanged(KeysName);
  181. OnPropertyChanged(ValuesName);
  182. }
  183. protected virtual void OnPropertyChanged(string propertyName)
  184. {
  185. if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  186. }
  187. private void OnCollectionChanged()
  188. {
  189. OnPropertyChanged();
  190. if (CollectionChanged != null) CollectionChanged(this, new specialized.NotifyCollectionChangedEventArgs(specialized.NotifyCollectionChangedAction.Reset));
  191. }
  192. private void OnCollectionChanged(specialized.NotifyCollectionChangedAction action, KeyValuePair<TKey, TValue> changedItem)
  193. {
  194. OnPropertyChanged();
  195. if (CollectionChanged != null) CollectionChanged(this, new specialized.NotifyCollectionChangedEventArgs(action, changedItem));
  196. }
  197. private void OnCollectionChanged(specialized.NotifyCollectionChangedAction action, KeyValuePair<TKey, TValue> newItem, KeyValuePair<TKey, TValue> oldItem)
  198. {
  199. OnPropertyChanged();
  200. if (CollectionChanged != null) CollectionChanged(this, new specialized.NotifyCollectionChangedEventArgs(action, newItem, oldItem));
  201. }
  202. private void OnCollectionChanged(specialized.NotifyCollectionChangedAction action, IList newItems)
  203. {
  204. OnPropertyChanged();
  205. if (CollectionChanged != null) CollectionChanged(this, new specialized.NotifyCollectionChangedEventArgs(action, newItems));
  206. }
  207. }
  208. }