init
This commit is contained in:
81
Common/DTS.Common.Utilities/SortableBindingList.cs
Normal file
81
Common/DTS.Common.Utilities/SortableBindingList.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace DTS.Common.Utilities
|
||||
{
|
||||
public class SortableBindingList<T> : BindingList<T>
|
||||
{
|
||||
private readonly Dictionary<Type, PropertyComparer<T>> _comparers;
|
||||
private bool _isSorted;
|
||||
private ListSortDirection _listSortDirection;
|
||||
private PropertyDescriptor _propertyDescriptor;
|
||||
|
||||
public SortableBindingList()
|
||||
: base(new List<T>())
|
||||
{
|
||||
_comparers = new Dictionary<Type, PropertyComparer<T>>();
|
||||
}
|
||||
|
||||
public SortableBindingList(IEnumerable<T> enumeration)
|
||||
: base(new List<T>(enumeration))
|
||||
{
|
||||
_comparers = new Dictionary<Type, PropertyComparer<T>>();
|
||||
}
|
||||
|
||||
protected override bool SupportsSortingCore => true;
|
||||
|
||||
protected override bool IsSortedCore => _isSorted;
|
||||
|
||||
protected override PropertyDescriptor SortPropertyCore => _propertyDescriptor;
|
||||
|
||||
protected override ListSortDirection SortDirectionCore => _listSortDirection;
|
||||
|
||||
protected override bool SupportsSearchingCore => true;
|
||||
|
||||
protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
|
||||
{
|
||||
var itemsList = (List<T>)Items;
|
||||
|
||||
var propertyType = property.PropertyType;
|
||||
PropertyComparer<T> comparer;
|
||||
if (!_comparers.TryGetValue(propertyType, out comparer))
|
||||
{
|
||||
comparer = new PropertyComparer<T>(property, direction);
|
||||
_comparers.Add(propertyType, comparer);
|
||||
}
|
||||
|
||||
comparer.SetPropertyAndDirection(property, direction);
|
||||
itemsList.Sort(comparer);
|
||||
|
||||
_propertyDescriptor = property;
|
||||
_listSortDirection = direction;
|
||||
_isSorted = true;
|
||||
|
||||
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
|
||||
}
|
||||
|
||||
protected override void RemoveSortCore()
|
||||
{
|
||||
_isSorted = false;
|
||||
_propertyDescriptor = base.SortPropertyCore;
|
||||
_listSortDirection = base.SortDirectionCore;
|
||||
|
||||
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
|
||||
}
|
||||
|
||||
protected override int FindCore(PropertyDescriptor property, object key)
|
||||
{
|
||||
var count = Count;
|
||||
for (var i = 0; i < count; ++i)
|
||||
{
|
||||
var element = this[i];
|
||||
var value = property.GetValue(element);
|
||||
if (value != null && value.Equals(key))
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user