using System; using System.Collections.Generic; using System.Collections.ObjectModel; namespace MonoGame.Extended.Collections { public class ObservableCollection : Collection, IObservableCollection { /// /// Initializes a new instance of the ObservableCollection class that is empty. /// public ObservableCollection() { } /// /// Initializes a new instance of the ObservableCollection class as a wrapper /// for the specified list. /// /// The list that is wrapped by the new collection. /// /// List is null. /// public ObservableCollection(IList list) : base(list) { } /// Raised when an item has been added to the collection public event EventHandler> ItemAdded; /// Raised when an item is removed from the collection public event EventHandler> ItemRemoved; /// Raised when the collection is about to be cleared /// /// This could be covered by calling ItemRemoved for each item currently /// contained in the collection, but it is often simpler and more efficient /// to process the clearing of the entire collection as a special operation. /// public event EventHandler Clearing; /// Raised when the collection has been cleared public event EventHandler Cleared; /// Removes all elements from the Collection protected override void ClearItems() { OnClearing(); base.ClearItems(); OnCleared(); } /// /// Inserts an element into the ObservableCollection at the specified index /// /// /// The object to insert. The value can be null for reference types. /// /// The zero-based index at which item should be inserted protected override void InsertItem(int index, T item) { base.InsertItem(index, item); OnAdded(item); } /// /// Removes the element at the specified index of the ObservableCollection /// /// The zero-based index of the element to remove protected override void RemoveItem(int index) { var item = base[index]; base.RemoveItem(index); OnRemoved(item); } /// Replaces the element at the specified index /// /// The new value for the element at the specified index. The value can be null /// for reference types /// /// The zero-based index of the element to replace protected override void SetItem(int index, T item) { var oldItem = base[index]; base.SetItem(index, item); OnRemoved(oldItem); OnAdded(item); } /// Fires the 'ItemAdded' event /// Item that has been added to the collection protected virtual void OnAdded(T item) { ItemAdded?.Invoke(this, new ItemEventArgs(item)); } /// Fires the 'ItemRemoved' event /// Item that has been removed from the collection protected virtual void OnRemoved(T item) { ItemRemoved?.Invoke(this, new ItemEventArgs(item)); } /// Fires the 'Clearing' event protected virtual void OnClearing() { Clearing?.Invoke(this, EventArgs.Empty); } /// Fires the 'Cleared' event protected virtual void OnCleared() { Cleared?.Invoke(this, EventArgs.Empty); } } }