namespace ServiceLib.Base; public class BulkObservableCollection : ObservableCollection { private bool _suppressNotification; public BulkObservableCollection() { } public BulkObservableCollection(IEnumerable collection) : base(collection) { } public BulkObservableCollection(List list) : base(list) { } protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { if (!_suppressNotification) { base.OnCollectionChanged(e); } } protected override void OnPropertyChanged(PropertyChangedEventArgs e) { if (!_suppressNotification) { base.OnPropertyChanged(e); } } public void AddRange(IEnumerable? collection) { if (collection == null) { return; } _suppressNotification = true; try { foreach (var item in collection) { Add(item); } } finally { _suppressNotification = false; OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count))); OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } } public bool Replace(T oldItem, T newItem) { var index = Items.IndexOf(oldItem); if (index < 0) { return false; } Items[index] = newItem; OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); OnCollectionChanged(new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Replace, newItem, oldItem, index)); return true; } public void ReplaceRange(IEnumerable? collection) { _suppressNotification = true; try { Items.Clear(); if (collection != null) { foreach (var item in collection) { Items.Add(item); } } } finally { _suppressNotification = false; OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count))); OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } } }