using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; namespace Neo.Afx.Collections { /// /// Represents a dynamic data collection that provides notifications when items get added, removed, or when the entire list is refreshed. /// /// The type of elements in the collection. public class TrulyObservableCollection : ObservableCollection { bool _suppressOnCollectionChanged; #region Initialization /// /// Initializes a new instance of the class. /// public TrulyObservableCollection() { } /// /// Initializes a new instance of the class. /// /// The items to be added. public TrulyObservableCollection(IEnumerable items) { AddRange(items); } #endregion #region AddRange /// /// Adds the given list of items. /// /// The items to be added. public void AddRange(IEnumerable items) { if(items == null) { throw new ArgumentNullException("items"); } if(!items.Any()) { return; } try { _suppressOnCollectionChanged = true; foreach(var item in items) { var localItem = item; // neccessary to avoid "access to modified closure" error Add(localItem); } } finally { _suppressOnCollectionChanged = false; OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } } #endregion /// /// Raises the event. /// /// The instance containing the event data. protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { if(!_suppressOnCollectionChanged) { base.OnCollectionChanged(e); } } } }