using System;
using System.Collections.ObjectModel;
namespace Framework.Support {
/// Collection that sends notifications when its contents change
/// Type of items that will be managed in the collection
public class ObservableCollection : Collection, IObservableCollection {
/// Called before the collection is cleared
public event Action Clearing;
/// Called after the collection has been cleared
public event Action Cleared;
/// Called when an item has been added into the collection
public event Action ItemAdded;
/// Called before an item gets removed from the collection
public event Action ItemRemoved;
/// Called when the collection is cleared of all its items
protected override void ClearItems() {
if(Clearing != null) {
Clearing();
}
base.ClearItems();
if(Cleared != null) {
Cleared();
}
}
/// Called when an item gets inserted into the collection
/// Index at which the item is inserted
/// Item that is being inserted
protected override void InsertItem(int index, TItem item) {
base.InsertItem(index, item);
if(ItemAdded != null) {
ItemAdded(item);
}
}
/// Called when an item is removed from the collection
/// Index at which the item is being removed
protected override void RemoveItem(int index) {
if(ItemRemoved != null) {
ItemRemoved(Items[index]);
}
base.RemoveItem(index);
}
/// Called when an item gets overwritten in the collection
/// Index at which an item is being overwritten
///
/// Item with which the item at the specified index is being overwritten
///
protected override void SetItem(int index, TItem item) {
if(ItemRemoved != null) {
ItemRemoved(Items[index]);
}
base.SetItem(index, item);
if(ItemAdded != null) {
ItemAdded(item);
}
}
}
} // namespace Framework.Support