forked from acnicholas/ReVInfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortableBindingList.cs
129 lines (113 loc) · 4.76 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
////=================================================================
//// SortedBindingList.cs
////=================================================================
//// PowerSDR is a C# implementation of a Software Defined Radio.
//// Copyright (C) 2004-2011 FlexRadio Systems
////
//// This program is free software; you can redistribute it and/or
//// modify it under the terms of the GNU General Public License
//// as published by the Free Software Foundation; either version 2
//// of the License, or (at your option) any later version.
////
//// This program is distributed in the hope that it will be useful,
//// but WITHOUT ANY WARRANTY; without even the implied warranty of
//// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//// GNU General Public License for more details.
////
//// You should have received a copy of the GNU General Public License
//// along with this program; if not, write to the Free Software
//// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
////
//// You may contact us via email at: gpl@flexradio.com.
//// Paper mail may be sent to:
//// FlexRadio Systems
//// 4616 W. Howard Lane Suite 1-150
//// Austin, TX 78728
//// USA
////=================================================================
//// ref: (http://www.tech.windowsapplication1.com/content/sortable-binding-list-custom-data-objects)
////=================================================================
namespace ReVInfo
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
public class SortableBindingListCollection<T> : System.ComponentModel.BindingList<T>
{
private bool sorted;
private System.ComponentModel.ListSortDirection sortDirectionm;
private System.ComponentModel.PropertyDescriptor sortPropertym;
public SortableBindingListCollection()
{
}
[SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists", Justification = "Borrowed code...")]
public SortableBindingListCollection(List<T> list) : base(list)
{
}
protected override System.ComponentModel.ListSortDirection SortDirectionCore
{
get {
return this.sortDirectionm; }
}
protected override System.ComponentModel.PropertyDescriptor SortPropertyCore
{
get { return this.sortPropertym; }
}
protected override bool IsSortedCore
{
get { return this.sorted; }
}
protected override bool SupportsSortingCore
{
get { return true; }
}
protected override void RemoveSortCore() {
this.sorted = false;
}
[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
protected override void ApplySortCore(System.ComponentModel.PropertyDescriptor prop, System.ComponentModel.ListSortDirection direction)
{
if (prop.PropertyType.GetInterface("IComparable") == null) {
return;
}
var list = this.Items as System.Collections.Generic.List<T>;
if (list == null) {
this.sorted = false;
} else {
var comparer = new PropertyComparer(prop.Name, direction);
list.Sort(comparer);
this.sorted = true;
this.sortDirectionm = direction;
this.sortPropertym = prop;
}
this.OnListChanged(new System.ComponentModel.ListChangedEventArgs(System.ComponentModel.ListChangedType.Reset, -1));
}
private class PropertyComparer : System.Collections.Generic.IComparer<T>
{
public PropertyComparer(string propName, System.ComponentModel.ListSortDirection direction)
{
this.PropInfo = typeof(T).GetProperty(propName);
this.Direction = direction;
}
private System.Reflection.PropertyInfo PropInfo
{
get; set;
}
private System.ComponentModel.ListSortDirection Direction
{
get; set;
}
public int Compare(T x, T y)
{
var xc = this.PropInfo.GetValue(x, null);
var yc = this.PropInfo.GetValue(y, null);
if (this.Direction == System.ComponentModel.ListSortDirection.Ascending) {
return System.Collections.Comparer.Default.Compare(xc, yc);
} else {
return System.Collections.Comparer.Default.Compare(yc, xc);
}
}
}
}
}