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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
namespace My.Collections.Generic
{
using System.Collections.Generic;
using System.ComponentModel;
public class SortedBindingList<T> : BindingList<T>
{
private ListSortDirection m_sortDirection;
private PropertyDescriptor m_propertyDescriptor;
private bool m_isSorted;
private const int NO_ITEM_INDEX = -1;
public SortedBindingList(IEnumerable<T> enumeration)
: base(new List<T>(enumeration))
{
}
protected override bool IsSortedCore
{
get
{
return m_isSorted;
}
}
protected override ListSortDirection SortDirectionCore
{
get
{
return m_sortDirection;
}
}
protected override PropertyDescriptor SortPropertyCore
{
get
{
return m_propertyDescriptor;
}
}
protected override bool SupportsSearchingCore
{
get
{
return true;
}
}
protected override bool SupportsSortingCore
{
get
{
return true;
}
}
protected override void ApplySortCore(PropertyDescriptor propertyDesciptor, ListSortDirection sortDirection)
{
m_isSorted = true;
m_sortDirection = sortDirection;
m_propertyDescriptor = propertyDesciptor;
var comparer = createComparer(propertyDesciptor, sortDirection);
sort(comparer);
}
protected virtual IComparer<T> createComparer(PropertyDescriptor property, ListSortDirection direction)
{
return new PropertyDescriptorComparer<T>(property, direction);
}
private void sort (IComparer <T> comparer)
{
((List<T>)Items).Sort(comparer);
OnListChanged (new ListChangedEventArgs (ListChangedType.Reset, NO_ITEM_INDEX));
}
protected override int FindCore(PropertyDescriptor property, object key)
{
int count = Count;
for (int itemIndex = 0; itemIndex < count; itemIndex++)
{
T item = this[itemIndex];
var itemValue = property.GetValue(item);
if (itemValue.Equals(key))
{
return itemIndex;
}
}
return NO_ITEM_INDEX;
}
protected override void RemoveSortCore()
{
m_isSorted = false;
m_sortDirection = base.SortDirectionCore;
m_propertyDescriptor = base.SortPropertyCore;
OnListChanged (new ListChangedEventArgs (ListChangedType.Reset, NO_ITEM_INDEX));
}
}
}
namespace My.Collections.Generic
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
public class PropertyDescriptorComparer<T> : IComparer<T>
{
private const int ASCENDING = 1;
private const int DESCENDING = -1;
private readonly int m_sortDirection;
private readonly PropertyDescriptor m_propertyDescriptor;
private readonly IComparer m_comparer;
public PropertyDescriptorComparer(PropertyDescriptor propertyDescriptor, ListSortDirection sortDirection)
{
m_propertyDescriptor = propertyDescriptor;
m_comparer = getComparerFromDescriptor();
m_sortDirection = sortDirection == ListSortDirection.Ascending ? ASCENDING : DESCENDING;
}
private IComparer getComparerFromDescriptor ()
{
Type comparerType = typeof (Comparer <>);
Type comparerForPropertyType = comparerType.MakeGenericType(m_propertyDescriptor.PropertyType);
return (IComparer) comparerForPropertyType.InvokeMember ("Default",
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.Static,
null, null, null);
}
public int Compare(T x, T y)
{
object xValue = m_propertyDescriptor.GetValue (x);
object yValue = m_propertyDescriptor.GetValue (y);
return m_sortDirection * m_comparer.Compare( xValue, yValue);
}
}
}
|