ObservableDictionary.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace PDF_Master.Helper
  9. {
  10. public class ObservableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, INotifyCollectionChanged, INotifyPropertyChanged
  11. {
  12. public ObservableDictionary()
  13. : base()
  14. { }
  15. private int _index;
  16. public event NotifyCollectionChangedEventHandler CollectionChanged;
  17. public event PropertyChangedEventHandler PropertyChanged;
  18. public new KeyCollection Keys
  19. {
  20. get { return base.Keys; }
  21. }
  22. public new ValueCollection Values
  23. {
  24. get { return base.Values; }
  25. }
  26. public new int Count
  27. {
  28. get { return base.Count; }
  29. }
  30. public new TValue this[TKey key]
  31. {
  32. get { return this.GetValue(key); }
  33. set { this.SetValue(key, value); }
  34. }
  35. public TValue this[int index]
  36. {
  37. get { return this.GetIndexValue(index); }
  38. set { this.SetIndexValue(index, value); }
  39. }
  40. public new void Add(TKey key, TValue value)
  41. {
  42. base.Add(key, value);
  43. this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, this.FindPair(key), _index));
  44. OnPropertyChanged("Keys");
  45. OnPropertyChanged("Values");
  46. OnPropertyChanged("Count");
  47. }
  48. public new void Clear()
  49. {
  50. base.Clear();
  51. this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
  52. OnPropertyChanged("Keys");
  53. OnPropertyChanged("Values");
  54. OnPropertyChanged("Count");
  55. }
  56. public new bool Remove(TKey key)
  57. {
  58. var pair = this.FindPair(key);
  59. if (base.Remove(key))
  60. {
  61. this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, pair, _index));
  62. OnPropertyChanged("Keys");
  63. OnPropertyChanged("Values");
  64. OnPropertyChanged("Count");
  65. return true;
  66. }
  67. return false;
  68. }
  69. protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
  70. {
  71. if (this.CollectionChanged != null)
  72. {
  73. this.CollectionChanged(this, e);
  74. }
  75. }
  76. protected void OnPropertyChanged(string propertyName)
  77. {
  78. if (this.PropertyChanged != null)
  79. {
  80. this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  81. }
  82. }
  83. #region private method
  84. private TValue GetIndexValue(int index)
  85. {
  86. for (int i = 0; i < this.Count; i++)
  87. {
  88. if (i == index)
  89. {
  90. var pair = this.ElementAt(i);
  91. return pair.Value;
  92. }
  93. }
  94. return default(TValue);
  95. }
  96. private void SetIndexValue(int index, TValue value)
  97. {
  98. try
  99. {
  100. var pair = this.ElementAtOrDefault(index);
  101. SetValue(pair.Key, value);
  102. }
  103. catch (Exception)
  104. {
  105. }
  106. }
  107. private TValue GetValue(TKey key)
  108. {
  109. if (base.ContainsKey(key))
  110. {
  111. return base[key];
  112. }
  113. else
  114. {
  115. return default(TValue);
  116. }
  117. }
  118. private void SetValue(TKey key, TValue value)
  119. {
  120. if (base.ContainsKey(key))
  121. {
  122. var pair = this.FindPair(key);
  123. int index = _index;
  124. base[key] = value;
  125. var newpair = this.FindPair(key);
  126. this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newpair, pair, index));
  127. OnPropertyChanged("Values");
  128. OnPropertyChanged("Item[]");
  129. }
  130. else
  131. {
  132. this.Add(key, value);
  133. }
  134. }
  135. private KeyValuePair<TKey, TValue> FindPair(TKey key)
  136. {
  137. _index = 0;
  138. foreach (var item in this)
  139. {
  140. if (item.Key.Equals(key))
  141. {
  142. return item;
  143. }
  144. _index++;
  145. }
  146. return default(KeyValuePair<TKey, TValue>);
  147. }
  148. private int IndexOf(TKey key)
  149. {
  150. int index = 0;
  151. foreach (var item in this)
  152. {
  153. if (item.Key.Equals(key))
  154. {
  155. return index;
  156. }
  157. index++;
  158. }
  159. return -1;
  160. }
  161. #endregion
  162. }
  163. }