BindableCollection.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. namespace Caliburn.Micro.Core {
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Collections.Specialized;
  5. using System.ComponentModel;
  6. /// <summary>
  7. /// A base collection class that supports automatic UI thread marshalling.
  8. /// </summary>
  9. /// <typeparam name="T">The type of elements contained in the collection.</typeparam>
  10. public class BindableCollection<T> : ObservableCollection<T>, IObservableCollection<T> {
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref = "Caliburn.Micro.BindableCollection&lt;T&gt;" /> class.
  13. /// </summary>
  14. public BindableCollection() {
  15. IsNotifying = true;
  16. }
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref = "Caliburn.Micro.BindableCollection&lt;T&gt;" /> class.
  19. /// </summary>
  20. /// <param name = "collection">The collection from which the elements are copied.</param>
  21. public BindableCollection(IEnumerable<T> collection)
  22. : base(collection) {
  23. IsNotifying = true;
  24. }
  25. /// <summary>
  26. /// Enables/Disables property change notification.
  27. /// </summary>
  28. public bool IsNotifying { get; set; }
  29. /// <summary>
  30. /// Notifies subscribers of the property change.
  31. /// </summary>
  32. /// <param name = "propertyName">Name of the property.</param>
  33. public virtual void NotifyOfPropertyChange(string propertyName) {
  34. if (IsNotifying)
  35. Execute.OnUIThread(() => OnPropertyChanged(new PropertyChangedEventArgs(propertyName)));
  36. }
  37. /// <summary>
  38. /// Raises a change notification indicating that all bindings should be refreshed.
  39. /// </summary>
  40. public void Refresh() {
  41. Execute.OnUIThread(() => {
  42. OnPropertyChanged(new PropertyChangedEventArgs("Count"));
  43. OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
  44. OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
  45. });
  46. }
  47. /// <summary>
  48. /// Inserts the item to the specified position.
  49. /// </summary>
  50. /// <param name = "index">The index to insert at.</param>
  51. /// <param name = "item">The item to be inserted.</param>
  52. protected override sealed void InsertItem(int index, T item) {
  53. Execute.OnUIThread(() => InsertItemBase(index, item));
  54. }
  55. /// <summary>
  56. /// Exposes the base implementation of the <see cref = "InsertItem" /> function.
  57. /// </summary>
  58. /// <param name = "index">The index.</param>
  59. /// <param name = "item">The item.</param>
  60. /// <remarks>
  61. /// Used to avoid compiler warning regarding unverifiable code.
  62. /// </remarks>
  63. protected virtual void InsertItemBase(int index, T item) {
  64. base.InsertItem(index, item);
  65. }
  66. /// <summary>
  67. /// Sets the item at the specified position.
  68. /// </summary>
  69. /// <param name = "index">The index to set the item at.</param>
  70. /// <param name = "item">The item to set.</param>
  71. protected override sealed void SetItem(int index, T item) {
  72. Execute.OnUIThread(() => SetItemBase(index, item));
  73. }
  74. /// <summary>
  75. /// Exposes the base implementation of the <see cref = "SetItem" /> function.
  76. /// </summary>
  77. /// <param name = "index">The index.</param>
  78. /// <param name = "item">The item.</param>
  79. /// <remarks>
  80. /// Used to avoid compiler warning regarding unverifiable code.
  81. /// </remarks>
  82. protected virtual void SetItemBase(int index, T item) {
  83. base.SetItem(index, item);
  84. }
  85. /// <summary>
  86. /// Removes the item at the specified position.
  87. /// </summary>
  88. /// <param name = "index">The position used to identify the item to remove.</param>
  89. protected override sealed void RemoveItem(int index) {
  90. Execute.OnUIThread(() => RemoveItemBase(index));
  91. }
  92. /// <summary>
  93. /// Exposes the base implementation of the <see cref = "RemoveItem" /> function.
  94. /// </summary>
  95. /// <param name = "index">The index.</param>
  96. /// <remarks>
  97. /// Used to avoid compiler warning regarding unverifiable code.
  98. /// </remarks>
  99. protected virtual void RemoveItemBase(int index) {
  100. base.RemoveItem(index);
  101. }
  102. /// <summary>
  103. /// Clears the items contained by the collection.
  104. /// </summary>
  105. protected override sealed void ClearItems() {
  106. Execute.OnUIThread(ClearItemsBase);
  107. }
  108. /// <summary>
  109. /// Exposes the base implementation of the <see cref = "ClearItems" /> function.
  110. /// </summary>
  111. /// <remarks>
  112. /// Used to avoid compiler warning regarding unverifiable code.
  113. /// </remarks>
  114. protected virtual void ClearItemsBase() {
  115. base.ClearItems();
  116. }
  117. /// <summary>
  118. /// Raises the <see cref = "E:System.Collections.ObjectModel.ObservableCollection`1.CollectionChanged" /> event with the provided arguments.
  119. /// </summary>
  120. /// <param name = "e">Arguments of the event being raised.</param>
  121. protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) {
  122. if (IsNotifying) {
  123. base.OnCollectionChanged(e);
  124. }
  125. }
  126. /// <summary>
  127. /// Raises the PropertyChanged event with the provided arguments.
  128. /// </summary>
  129. /// <param name = "e">The event data to report in the event.</param>
  130. protected override void OnPropertyChanged(PropertyChangedEventArgs e) {
  131. if (IsNotifying) {
  132. base.OnPropertyChanged(e);
  133. }
  134. }
  135. /// <summary>
  136. /// Adds the range.
  137. /// </summary>
  138. /// <param name = "items">The items.</param>
  139. public virtual void AddRange(IEnumerable<T> items) {
  140. Execute.OnUIThread(() => {
  141. var previousNotificationSetting = IsNotifying;
  142. IsNotifying = false;
  143. var index = Count;
  144. foreach (var item in items) {
  145. InsertItemBase(index, item);
  146. index++;
  147. }
  148. IsNotifying = previousNotificationSetting;
  149. OnPropertyChanged(new PropertyChangedEventArgs("Count"));
  150. OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
  151. OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
  152. });
  153. }
  154. /// <summary>
  155. /// Removes the range.
  156. /// </summary>
  157. /// <param name = "items">The items.</param>
  158. public virtual void RemoveRange(IEnumerable<T> items) {
  159. Execute.OnUIThread(() => {
  160. var previousNotificationSetting = IsNotifying;
  161. IsNotifying = false;
  162. foreach (var item in items) {
  163. var index = IndexOf(item);
  164. if (index >= 0) {
  165. RemoveItemBase(index);
  166. }
  167. }
  168. IsNotifying = previousNotificationSetting;
  169. OnPropertyChanged(new PropertyChangedEventArgs("Count"));
  170. OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
  171. OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
  172. });
  173. }
  174. }
  175. }