ObservableDictionary.cs 4.7 KB

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