-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathSortableBindingList.cs
191 lines (149 loc) · 6.82 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace fCraft.ConfigGUI {
[Serializable]
// SortableBindingList by Tim Van Wassenhove, http://www.timvw.be/presenting-the-sortablebindinglistt/
public sealed class SortableBindingList<T> : BindingList<T> {
private bool isSorted;
private ListSortDirection dir = ListSortDirection.Ascending;
[NonSerialized]
private PropertyDescriptor sort;
#region BindingList<T> Public Sorting API
public void Sort() {
ApplySortCore( sort, dir );
}
public void Sort( string property ) {
/* Get the PD */
sort = FindPropertyDescriptor( property );
/* Sort */
ApplySortCore( sort, dir );
}
public void Sort( string property, ListSortDirection direction ) {
/* Get the sort property */
sort = FindPropertyDescriptor( property );
dir = direction;
/* Sort */
ApplySortCore( sort, dir );
}
#endregion BindingList<T> Public Sorting API
#region BindingList<T> Sorting Overrides
protected override bool SupportsSortingCore {
get { return true; }
}
protected override void ApplySortCore( PropertyDescriptor prop, ListSortDirection direction ) {
List<T> items = Items as List<T>;
if ( null != items ) {
PropertyComparer<T> pc = new PropertyComparer<T>( prop, direction );
items.Sort( pc );
/* Set sorted */
isSorted = true;
} else {
/* Set sorted */
isSorted = false;
}
}
protected override bool IsSortedCore {
get { return isSorted; }
}
protected override void RemoveSortCore() {
isSorted = false;
}
#endregion BindingList<T> Sorting Overrides
#region BindingList<T> Private Sorting API
private static PropertyDescriptor FindPropertyDescriptor( string property ) {
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties( typeof( T ) );
PropertyDescriptor prop = pdc.Find( property, true );
return prop;
}
#endregion BindingList<T> Private Sorting API
#region PropertyComparer<TKey>
internal sealed class PropertyComparer<TKey> : IComparer<TKey> {
/*
* The following code contains code implemented by Rockford Lhotka:
* //msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet01272004.asp" href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet01272004.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet01272004.asp
*/
private readonly PropertyDescriptor property;
private readonly ListSortDirection direction;
public PropertyComparer( PropertyDescriptor property, ListSortDirection direction ) {
this.property = property;
this.direction = direction;
}
public int Compare( TKey xVal, TKey yVal ) {
/* Get property values */
object xValue = GetPropertyValue( xVal, property.Name );
object yValue = GetPropertyValue( yVal, property.Name );
foreach ( Attribute att in property.Attributes ) {
var sortableAtt = att as SortablePropertyAttribute;
if ( sortableAtt != null ) {
int comparisonResult = sortableAtt.Compare( property.Name, xVal, yVal );
if ( direction == ListSortDirection.Ascending ) {
return comparisonResult;
} else {
return -comparisonResult;
}
}
}
/* Determine sort order */
if ( direction == ListSortDirection.Ascending ) {
return CompareAscending( xValue, yValue );
} else {
return CompareDescending( xValue, yValue );
}
}
public bool Equals( TKey xVal, TKey yVal ) {
return xVal.Equals( yVal );
}
public int GetHashCode( TKey obj ) {
return obj.GetHashCode();
}
/* Compare two property values of any type */
private static int CompareAscending( object xValue, object yValue ) {
int result;
/* If values implement IComparer */
if ( xValue is IComparable ) {
result = ( ( IComparable )xValue ).CompareTo( yValue );
}
/* If values don't implement IComparer but are equivalent */
else if ( xValue.Equals( yValue ) ) {
result = 0;
}
/* Values don't implement IComparer and are not equivalent, so compare as string values */
else
result = xValue.ToString().CompareTo( yValue.ToString() );
/* Return result */
return result;
}
private static int CompareDescending( object xValue, object yValue ) {
/* Return result adjusted for ascending or descending sort order ie
multiplied by 1 for ascending or -1 for descending */
return CompareAscending( xValue, yValue ) * -1;
}
private static object GetPropertyValue( TKey value, string property ) {
/* Get property */
PropertyInfo propertyInfo = value.GetType().GetProperty( property );
/* Return value */
return propertyInfo.GetValue( value, null );
}
}
#endregion PropertyComparer<TKey>
}
[AttributeUsage( AttributeTargets.Property )]
public sealed class SortablePropertyAttribute : Attribute {
public SortablePropertyAttribute( Type type, string comparerMethodName ) {
if ( type == null )
throw new ArgumentNullException( "type" );
if ( comparerMethodName == null )
throw new ArgumentNullException( "comparerMethodName" );
method = type.GetMethod( comparerMethodName );
if ( method == null )
throw new ArgumentException( "No such method", "comparerMethodName" );
}
private readonly MethodInfo method;
public int Compare( string propertyName, object a, object b ) {
object[] methodArgs = new[] { propertyName, a, b };
return ( int )method.Invoke( null, methodArgs );
}
}
}