ObservableRangeCollection.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Collections.Specialized;
  5. using System.Linq;
  6. namespace MECF.Framework.UI.Client.ClientBase.Collections
  7. {
  8. /// <summary>
  9. /// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
  10. /// </summary>
  11. /// <typeparam name="T"></typeparam>
  12. public class ObservableRangeCollection<T> : ObservableCollection<T>
  13. {
  14. #region Constructors
  15. /// <summary>
  16. /// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class.
  17. /// </summary>
  18. public ObservableRangeCollection()
  19. {
  20. }
  21. /// <summary>
  22. /// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class that contains elements copied from the specified collection.
  23. /// </summary>
  24. /// <param name="collection">collection: The collection from which the elements are copied.</param>
  25. /// <exception cref="System.ArgumentNullException">The collection parameter cannot be null.</exception>
  26. public ObservableRangeCollection(IEnumerable<T> collection)
  27. : base(collection)
  28. {
  29. }
  30. #endregion
  31. /// <summary>
  32. /// Adds the elements of the specified collection to the end of the ObservableCollection(Of T).
  33. /// </summary>
  34. public virtual void AddRange(IEnumerable<T> collection)
  35. {
  36. if (collection == null) throw new ArgumentNullException(nameof(collection));
  37. var startIndex = Count;
  38. var enumerable = collection.ToList();
  39. foreach (var i in enumerable)
  40. Items.Add(i);
  41. OnCollectionChanged(
  42. new NotifyCollectionChangedEventArgs(
  43. NotifyCollectionChangedAction.Reset));
  44. }
  45. /// <summary>
  46. /// Removes the first occurence of each item in the specified collection from ObservableCollection(Of T).
  47. /// </summary>
  48. public virtual void RemoveRange(IEnumerable<T> collection)
  49. {
  50. if (collection == null) throw new ArgumentNullException(nameof(collection));
  51. foreach (var i in collection)
  52. Items.Remove(i);
  53. OnCollectionChanged(
  54. new NotifyCollectionChangedEventArgs(
  55. NotifyCollectionChangedAction.Reset));
  56. }
  57. /// <summary>
  58. /// Clears the current collection and replaces it with the specified item.
  59. /// </summary>
  60. public virtual void Replace(T item)
  61. {
  62. ReplaceRange(new[] {item});
  63. }
  64. /// <summary>
  65. /// Clears the current collection and replaces it with the specified collection.
  66. /// </summary>
  67. public void ReplaceRange(IEnumerable<T> collection)
  68. {
  69. if (collection == null) throw new ArgumentNullException(nameof(collection));
  70. Items.Clear();
  71. foreach (var i in collection) Items.Add(i);
  72. OnCollectionChanged(
  73. new NotifyCollectionChangedEventArgs(
  74. NotifyCollectionChangedAction.Reset));
  75. }
  76. }
  77. }