using System; using System.Collections.Generic; using System.ComponentModel; namespace DTS.Common.Utilities { public class SortableBindingList : BindingList { private readonly Dictionary> _comparers; private bool _isSorted; private ListSortDirection _listSortDirection; private PropertyDescriptor _propertyDescriptor; public SortableBindingList() : base(new List()) { _comparers = new Dictionary>(); } public SortableBindingList(IEnumerable enumeration) : base(new List(enumeration)) { _comparers = new Dictionary>(); } 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)Items; var propertyType = property.PropertyType; PropertyComparer comparer; if (!_comparers.TryGetValue(propertyType, out comparer)) { comparer = new PropertyComparer(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; } } }