using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; namespace Neo.Afx.Collections { /// /// Represents an observable collection of objects. /// public class ObservableCollection : ICollection, INotifyCollectionChanged { readonly List _contents = new List(); /// /// Occurs when the items list of the collection has changed, or the collection is reset. /// public event NotifyCollectionChangedEventHandler CollectionChanged; /// /// Initializes a new instance of the class. /// public ObservableCollection() { } /// /// Initializes a new instance of the class. /// /// The collection to be added. public ObservableCollection(IEnumerable collection) { AddRange(collection); } #region IEnumerable Members #region GetEnumerator /// /// Returns an enumerator that iterates through a collection. /// /// /// An object that can be used to iterate through the collection. /// public IEnumerator GetEnumerator() { return _contents.GetEnumerator(); } #endregion #endregion #region ICollection Members #region CopyTo /// /// Copies the elements of the to an , starting at a particular index. /// /// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing. /// The zero-based index in at which copying begins. /// /// is null. /// /// /// is less than zero. /// /// /// is multidimensional.-or- The number of elements in the source is greater than the available space from to the end of the destination .-or- The type of the source cannot be cast automatically to the type of the destination /// public void CopyTo(Array array, int index) { var j = index; for(var i = 0; i < Count; i++) { array.SetValue(_contents[i], j); j++; } } #endregion #region Count /// /// Gets the number of elements contained in the . /// /// /// The number of elements contained in the . public int Count { get { return _contents.Count; } } #endregion #region IsSynchronized /// /// Gets a value indicating whether access to the is synchronized (thread safe). /// /// false /// true if access to the is synchronized (thread safe); otherwise, false. public bool IsSynchronized { get { return false; } } #endregion #region SyncRoot /// /// Gets an object that can be used to synchronize access to the . /// /// Return the current instance since the underlying store is not publicly available /// /// /// /// An object that can be used to synchronize access to the . public object SyncRoot { get { return this; } } #endregion #endregion #region AddRange /// /// Adds the specified collection. /// /// The collection to be added. public void AddRange(IEnumerable collection) { _contents.AddRange(collection.Cast()); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } #endregion #region Clear /// /// Clears the collection. /// public void Clear() { _contents.Clear(); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } #endregion /// /// Raises the event. /// /// The instance containing the event data. protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { if(CollectionChanged != null) { CollectionChanged(this, e); } } } }