-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortableBindingList.cs
169 lines (145 loc) · 5.31 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
// https://garafu.blogspot.com/2016/09/cs-sorablebindinglist.html
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Suconbu.Sumacon
{
/// <summary>
/// ソート可能なバインディングリストクラス
/// </summary>
/// <typeparam name="T">リスト内容</typeparam>
public class SortableBindingList<T> : BindingList<T>
where T : class
{
/// <summary>
/// ソート済みかどうか
/// </summary>
private bool isSorted;
/// <summary>
/// 並べ替え操作の方向
/// </summary>
private ListSortDirection sortDirection = ListSortDirection.Ascending;
/// <summary>
/// ソートを行う抽象化プロパティ
/// </summary>
private PropertyDescriptor sortProperty;
/// <summary>
/// SortableBindingList クラス の 新しいインスタンス を初期化します。
/// </summary>
public SortableBindingList()
{
}
/// <summary>
/// 指定した リストクラス を利用して SortableBindingList クラス の 新しいインスタンス を初期化します。
/// </summary>
/// <param name="list">SortableBindingList に格納される System.Collection.Generic.IList</param>
public SortableBindingList(IList<T> list)
: base(list)
{
}
/// <summary>
/// リストがソートをサポートしているかどうかを示す値を取得します。
/// </summary>
protected override bool SupportsSortingCore
{
get { return true; }
}
/// <summary>
/// リストがソートされたかどうかを示す値を取得します。
/// </summary>
protected override bool IsSortedCore
{
get { return this.isSorted; }
}
/// <summary>
/// ソートされたリストの並べ替え操作の方向を取得します。
/// </summary>
protected override ListSortDirection SortDirectionCore
{
get { return this.sortDirection; }
}
/// <summary>
/// ソートに利用する抽象化プロパティを取得します。
/// </summary>
protected override PropertyDescriptor SortPropertyCore
{
get { return this.sortProperty; }
}
/// <summary>
/// ApplySortCore で適用されたソートに関する情報を削除します。
/// </summary>
protected override void RemoveSortCore()
{
this.sortDirection = ListSortDirection.Ascending;
this.sortProperty = null;
this.isSorted = false;
}
/// <summary>
/// 指定されたプロパティおよび方向でソートを行います。
/// </summary>
/// <param name="prop">抽象化プロパティ</param>
/// <param name="direction">並べ替え操作の方向</param>
protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
// ソートに使う情報を記録
this.sortProperty = prop;
this.sortDirection = direction;
// ソートを行うリストを取得
var list = Items as List<T>;
if (list == null)
{
return;
}
// ソート処理
list.Sort(this.Compare);
// ソート完了を記録
this.isSorted = true;
// ListChanged イベントを発生させます
this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
/// <summary>
/// 比較処理を行います。
/// </summary>
/// <param name="lhs">左側の値</param>
/// <param name="rhs">右側の値</param>
/// <returns>比較結果</returns>
private int Compare(T lhs, T rhs)
{
// 比較を行う
var result = this.OnComparison(lhs, rhs);
// 昇順の場合 そのまま、降順の場合 反転させる
return (this.sortDirection == ListSortDirection.Ascending) ? result : -result;
}
/// <summary>
/// 昇順として比較処理を行います。
/// </summary>
/// <param name="lhs">左側の値</param>
/// <param name="rhs">右側の値</param>
/// <returns>比較結果</returns>
private int OnComparison(T lhs, T rhs)
{
object lhsValue = (lhs == null) ? null : this.sortProperty.GetValue(lhs);
object rhsValue = (rhs == null) ? null : this.sortProperty.GetValue(rhs);
if (lhsValue == null)
{
return (rhsValue == null) ? 0 : -1;
}
if (rhsValue == null)
{
return 1;
}
if (lhsValue is IComparable)
{
return ((IComparable)lhsValue).CompareTo(rhsValue);
}
if (lhsValue.Equals(rhsValue))
{
return 0;
}
return lhsValue.ToString().CompareTo(rhsValue.ToString());
}
}
}