init
This commit is contained in:
55
Common/DTS.Common/Classes/BulkObservableCollection.cs
Normal file
55
Common/DTS.Common/Classes/BulkObservableCollection.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user