ObservableDictionary.cs 7.9 KB

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