Skip to content

Instantly share code, notes, and snippets.

@dotMorten
Created November 26, 2015 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotMorten/7c2a4e790b9604e9efd9 to your computer and use it in GitHub Desktop.
Save dotMorten/7c2a4e790b9604e9efd9 to your computer and use it in GitHub Desktop.
Observable Collection supporting range adds efficiently without causing a lot of UI updates
public class ObservableCollection2<T> : ObservableCollection<T>
{
public void AddRange(IEnumerable<T> items)
{
InsertRange(Count, items);
}
public void InsertRange(int index, IEnumerable<T> items)
{
int count = 0;
foreach(var item in items)
{
base.Items.Insert(index + (count++), item);
}
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Items[]"));
OnCollectionChanged(
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,
changedItems: (System.Collections.IList)items.ToList(),
startingIndex: index));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment