56 lines
1.9 KiB
Plaintext
56 lines
1.9 KiB
Plaintext
using DTS.Common.Utilities.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Collections.Specialized;
|
|
using System.ComponentModel;
|
|
|
|
namespace DTS.Common.Classes
|
|
{
|
|
/// <summary>
|
|
/// this is a helper class to avoid performance issues
|
|
/// adding or removing a bulk sequence of items from the collection
|
|
/// by firing just one notification rather than n
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public class BulkObservableCollection<T> : ObservableCollection<T>
|
|
{
|
|
public void AddRange(IEnumerable<T> items)
|
|
{
|
|
try
|
|
{
|
|
// Turn off notifications (check if anyone is listening first)
|
|
CheckReentrancy();
|
|
foreach (var item in items) { Items.Add(item); }
|
|
// Fire a single notification that the entire "Reset" happened
|
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
|
OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
|
|
}
|
|
catch( Exception ex)
|
|
{
|
|
APILogger.Log(ex);
|
|
}
|
|
}
|
|
public void RemoveRange(IEnumerable<T> itemsToRemove)
|
|
{
|
|
try
|
|
{
|
|
CheckReentrancy();
|
|
|
|
foreach (var item in itemsToRemove)
|
|
{
|
|
Items.Remove(item);
|
|
}
|
|
|
|
// Fire one single notification for the entire operation
|
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
|
OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
|
|
}
|
|
catch( Exception ex)
|
|
{
|
|
APILogger.Log(ex);
|
|
}
|
|
}
|
|
}
|
|
}
|