ObservableCollection.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. namespace MonoGame.Extended.Collections
  5. {
  6. public class ObservableCollection<T> : Collection<T>, IObservableCollection<T>
  7. {
  8. /// <summary>
  9. /// Initializes a new instance of the ObservableCollection class that is empty.
  10. /// </summary>
  11. public ObservableCollection()
  12. {
  13. }
  14. /// <summary>
  15. /// Initializes a new instance of the ObservableCollection class as a wrapper
  16. /// for the specified list.
  17. /// </summary>
  18. /// <param name="list">The list that is wrapped by the new collection.</param>
  19. /// <exception cref="System.ArgumentNullException">
  20. /// List is null.
  21. /// </exception>
  22. public ObservableCollection(IList<T> list) : base(list)
  23. {
  24. }
  25. /// <summary>Raised when an item has been added to the collection</summary>
  26. public event EventHandler<ItemEventArgs<T>> ItemAdded;
  27. /// <summary>Raised when an item is removed from the collection</summary>
  28. public event EventHandler<ItemEventArgs<T>> ItemRemoved;
  29. /// <summary>Raised when the collection is about to be cleared</summary>
  30. /// <remarks>
  31. /// This could be covered by calling ItemRemoved for each item currently
  32. /// contained in the collection, but it is often simpler and more efficient
  33. /// to process the clearing of the entire collection as a special operation.
  34. /// </remarks>
  35. public event EventHandler Clearing;
  36. /// <summary>Raised when the collection has been cleared</summary>
  37. public event EventHandler Cleared;
  38. /// <summary>Removes all elements from the Collection</summary>
  39. protected override void ClearItems()
  40. {
  41. OnClearing();
  42. base.ClearItems();
  43. OnCleared();
  44. }
  45. /// <summary>
  46. /// Inserts an element into the ObservableCollection at the specified index
  47. /// </summary>
  48. /// <param name="index">
  49. /// The object to insert. The value can be null for reference types.
  50. /// </param>
  51. /// <param name="item">The zero-based index at which item should be inserted</param>
  52. protected override void InsertItem(int index, T item)
  53. {
  54. base.InsertItem(index, item);
  55. OnAdded(item);
  56. }
  57. /// <summary>
  58. /// Removes the element at the specified index of the ObservableCollection
  59. /// </summary>
  60. /// <param name="index">The zero-based index of the element to remove</param>
  61. protected override void RemoveItem(int index)
  62. {
  63. var item = base[index];
  64. base.RemoveItem(index);
  65. OnRemoved(item);
  66. }
  67. /// <summary>Replaces the element at the specified index</summary>
  68. /// <param name="index">
  69. /// The new value for the element at the specified index. The value can be null
  70. /// for reference types
  71. /// </param>
  72. /// <param name="item">The zero-based index of the element to replace</param>
  73. protected override void SetItem(int index, T item)
  74. {
  75. var oldItem = base[index];
  76. base.SetItem(index, item);
  77. OnRemoved(oldItem);
  78. OnAdded(item);
  79. }
  80. /// <summary>Fires the 'ItemAdded' event</summary>
  81. /// <param name="item">Item that has been added to the collection</param>
  82. protected virtual void OnAdded(T item)
  83. {
  84. ItemAdded?.Invoke(this, new ItemEventArgs<T>(item));
  85. }
  86. /// <summary>Fires the 'ItemRemoved' event</summary>
  87. /// <param name="item">Item that has been removed from the collection</param>
  88. protected virtual void OnRemoved(T item)
  89. {
  90. ItemRemoved?.Invoke(this, new ItemEventArgs<T>(item));
  91. }
  92. /// <summary>Fires the 'Clearing' event</summary>
  93. protected virtual void OnClearing()
  94. {
  95. Clearing?.Invoke(this, EventArgs.Empty);
  96. }
  97. /// <summary>Fires the 'Cleared' event</summary>
  98. protected virtual void OnCleared()
  99. {
  100. Cleared?.Invoke(this, EventArgs.Empty);
  101. }
  102. }
  103. }