-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSortableBindingList.cs
87 lines (65 loc) · 2.16 KB
/
SortableBindingList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace SDRSharp.Tetra
{
public class SortableBindingList<T> : BindingList<T>
{
private bool _isSorted = false;
private PropertyDescriptor _sortProperty;
private ListSortDirection _sortDirection;
protected override bool SupportsSortingCore
{
get { return true; }
}
protected override ListSortDirection SortDirectionCore
{
get { return _sortDirection; }
}
protected override PropertyDescriptor SortPropertyCore
{
get { return _sortProperty; }
}
protected override bool IsSortedCore
{
get { return _isSorted; }
}
protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
{
var items = (List<T>)this.Items;
if (items != null)
{
var pc = new SortableBindingListComparer<T>(property.Name, direction);
items.Sort(pc);
_isSorted = true;
}
else
{
_isSorted = false;
}
_sortProperty = property;
_sortDirection = direction;
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
}
public class SortableBindingListComparer<T> : IComparer<T>
{
private PropertyInfo _sortProperty;
private ListSortDirection _sortDirection;
public SortableBindingListComparer(string sortProperty, ListSortDirection sortDirection)
{
_sortProperty = typeof(T).GetProperty(sortProperty);
_sortDirection = sortDirection;
}
public int Compare(T x, T y)
{
IComparable oX = (IComparable)_sortProperty.GetValue(x, null);
IComparable oY = (IComparable)_sortProperty.GetValue(y, null);
if (_sortDirection == ListSortDirection.Ascending)
return oX.CompareTo(oY);
else
return oY.CompareTo(oX);
}
}
}