-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortableBindingList.cs
124 lines (114 loc) · 3.61 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
namespace Microsoft.LiveMeeting.RecordingExporter
{
public class SortableBindingList<T> : BindingList<T>
{
private ListSortDirection _sortDirection;
private PropertyDescriptor _sortProperty;
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
{
for (int i = 0; i < Items.Count - 1; ++i)
{
T lhs = Items[i];
T rhs = Items[i + 1];
PropertyDescriptor property = SortPropertyCore;
if (property != null)
{
object lhsValue = lhs == null ? null : property.GetValue(lhs);
object rhsValue = rhs == null ? null : property.GetValue(rhs);
int result;
if (lhsValue == null)
{
result = -1;
}
else if (rhsValue == null)
{
result = 1;
}
else
{
result = Comparer.Default.Compare(lhsValue, rhsValue);
}
if (SortDirectionCore == ListSortDirection.Descending)
{
result = -result;
}
if (result >= 0)
{
return false;
}
}
}
return true;
}
}
protected override void ApplySortCore(
PropertyDescriptor prop,
ListSortDirection direction)
{
_sortProperty = prop;
_sortDirection = direction;
List<T> list = (List<T>)Items;
list.Sort(delegate (T lhs, T rhs)
{
if (_sortProperty != null)
{
object lhsValue = lhs == null ? null : _sortProperty.GetValue(lhs);
object rhsValue = rhs == null ? null : _sortProperty.GetValue(rhs);
int result;
if (lhsValue == null)
{
result = -1;
}
else if (rhsValue == null)
{
result = 1;
}
else
{
result = Comparer.Default.Compare(lhsValue, rhsValue);
}
if (_sortDirection == ListSortDirection.Descending)
{
result = -result;
}
return result;
}
else
{
return 0;
}
});
}
protected override void RemoveSortCore()
{
_sortDirection = ListSortDirection.Ascending;
_sortProperty = null;
}
}
}