diff --git a/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs b/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs index 982829978e72..88f3a837e00c 100644 --- a/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs +++ b/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs @@ -51,21 +51,6 @@ public ObservableCollection(IEnumerable collection) CopyFrom(collection); } - private void CopyFrom(IEnumerable collection) - { - IList items = Items; - if (collection != null && items != null) - { - using (IEnumerator enumerator = collection.GetEnumerator()) - { - while (enumerator.MoveNext()) - { - items.Add(enumerator.Current); - } - } - } - } - #endregion Constructors @@ -84,6 +69,29 @@ public void Move(int oldIndex, int newIndex) { MoveItem(oldIndex, newIndex); } + + /// + /// Adds a range of objects but does not raise the OnCollectionChanged event until all objects are added. + /// + /// The set of objects to be added. + /// Determines whether or not to remove any existing items in the collection first. Defaults to False. + public void AddRange(IEnumerable collection, bool clearFirst = false) + { + CheckReentrancy(); + + if (clearFirst) Items.Clear(); + + var newIndex = Items.Count; + foreach (var item in collection) + { + base.InsertItem(newIndex, item); + newIndex++; + } + + OnPropertyChanged(CountString); + OnPropertyChanged(IndexerName); + OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, new List(collection))); + } #endregion Public Methods @@ -292,6 +300,24 @@ protected void CheckReentrancy() //------------------------------------------------------ #region Private Methods + /// + /// Copies an existing collection into this one. + /// + private void CopyFrom(IEnumerable collection) + { + IList items = Items; + if (collection != null && items != null) + { + using (IEnumerator enumerator = collection.GetEnumerator()) + { + while (enumerator.MoveNext()) + { + items.Add(enumerator.Current); + } + } + } + } + /// /// Helper to raise a PropertyChanged event />). ///