This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
/
CheckableGroupBehavior.cs
297 lines (249 loc) · 12.7 KB
/
CheckableGroupBehavior.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using DevExpress.Mvvm.UI.Interactivity;
using DevExpress.Xpf.Grid;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Markup;
namespace DevExpress.Example03 {
public class CheckableGroupBehavior : Behavior<GridControl> {
#region Fields
protected bool _LocalSet;
#endregion Fields
#region Checkable
public string CheckableProperty {
get { return (string)GetValue(CheckablePropertyProperty); }
set { SetValue(CheckablePropertyProperty, value); }
}
public static readonly DependencyProperty CheckablePropertyProperty =
DependencyProperty.Register("CheckableProperty", typeof(string), typeof(CheckableGroupBehavior), new PropertyMetadata(string.Empty));
#endregion Checkable
#region CheckList
public static Dictionary<int, bool?> GetCheckList(DependencyObject obj) {
return (Dictionary<int, bool?>)obj.GetValue(CheckListProperty);
}
public static void SetCheckList(DependencyObject obj, Dictionary<int, bool?> value) {
obj.SetValue(CheckListProperty, value);
}
public static readonly DependencyProperty CheckListProperty =
DependencyProperty.RegisterAttached("CheckList", typeof(Dictionary<int, bool?>), typeof(CheckableGroupBehavior), new PropertyMetadata(new Dictionary<int, bool?>(), IsCheckListPropertyChanged));
#endregion CheckList
public static event Action<DependencyObject, DependencyPropertyChangedEventArgs> IsCheckListChanged;
protected static void IsCheckListPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) {
if(IsCheckListChanged != null) {
IsCheckListChanged(obj, args);
}
}
protected void OnIsCheckListChanged(object sender, DependencyPropertyChangedEventArgs args) {
if(args.NewValue == null) {
return;
}
if(this._LocalSet) { return; }
this._LocalSet = true;
var grid = this.AssociatedObject;
var table = this.AssociatedObject.View as TableView;
var rowHandle = grid.GetSelectedRowHandles().First();
var checkStates = (args.NewValue as Dictionary<int, bool?>);
int groupLevel = grid.GetRowLevelByRowHandle(rowHandle);
bool? value = checkStates.Keys.Contains(groupLevel) ? checkStates[groupLevel] : false;
this.WorkOutGroup(rowHandle, value);
this._LocalSet = false;
}
protected override void OnAttached() {
base.OnAttached();
if(this.AssociatedType != typeof(GridControl) || this.CheckableProperty == string.Empty) {
return;
}
this.AssociatedObject.Loaded += GridLoaded;
}
protected void AttachItem(object obj) {
var iNotifyPropertyChanged = (INotifyPropertyChanged)obj;
if(iNotifyPropertyChanged != null) {
iNotifyPropertyChanged.PropertyChanged += this.PropertyChanged;
}
}
protected void GridLoaded(object sender, RoutedEventArgs e) {
if(this.AssociatedObject.View.GetType() == typeof(TableView)) {
(this.AssociatedObject.View as TableView).GroupValueTemplate = this.GetGroupValueTemplate();
}
if(this.AssociatedObject.GroupCount > 0) {
this.AssociatedObject_EndGrouping(null, null);
}
var items = this.AssociatedObject.ItemsSource as IEnumerable<object>;
if(items != null) {
foreach(var item in items) {
this.AttachItem(item);
}
}
INotifyCollectionChanged collection = (INotifyCollectionChanged)this.AssociatedObject.ItemsSource;
if(collection != null) {
collection.CollectionChanged += CollectionChanged;
}
IsCheckListChanged += OnIsCheckListChanged;
this.AssociatedObject.EndGrouping += AssociatedObject_EndGrouping;
(this.AssociatedObject.View as TableView).CellValueChanged += GridCellValueChanged;
(this.AssociatedObject.View as TableView).CellValueChanging += CellValueChanging;
}
void AssociatedObject_EndGrouping(object sender, RoutedEventArgs e) {
if(this.AssociatedObject.GroupCount == 0) { return; }
var res = this.GetAllRowHandles();
List<int> parents = new List<int>();
int j = res.Count;
while(--j >= 0) {
var parent = this.AssociatedObject.GetParentRowHandle(res[j]);
if(!parents.Contains(parent)) {
parents.Add(parent);
do {
parent = this.AssociatedObject.GetParentRowHandle(parent);
if(!parents.Contains(parent) && this.AssociatedObject.IsValidRowHandle(parent)) {
parents.Add(parent);
}
} while(this.AssociatedObject.IsValidRowHandle(parent));
}
}
int g = this.AssociatedObject.GroupCount;
while(--g >= 0) {
var levelItems = parents.Where(c => this.AssociatedObject.GetRowLevelByRowHandle(c) == g);
if(levelItems == null) { continue; }
foreach(var item in levelItems) {
var b = this.GetRowChildrenState(item);
var dic = (Dictionary<int, bool?>)this.AssociatedObject.GetRowState(item, true).GetValue(CheckableGroupBehavior.CheckListProperty);
dic[g] = b;
this._LocalSet = true;
this.AssociatedObject.GetRowState(item, true).SetValue(CheckableGroupBehavior.CheckListProperty, new Dictionary<int, bool?>(dic));
this._LocalSet = false;
}
}
}
protected List<int> GetAllRowHandles() {
List<int> result = new List<int>();
foreach(var item in this.AssociatedObject.ItemsSource as IEnumerable<object>) {
int handle = this.AssociatedObject.FindRowByValue("", item);
result.Add(handle);
}
return result;
}
protected void CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
switch(e.Action) {
case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
foreach(var item in e.NewItems) {
this.AttachItem(item);
}
break;
default:
break;
}
}
public void PropertyChanged(object obj, PropertyChangedEventArgs args) {
if(!this._LocalSet && args.PropertyName == this.CheckableProperty) {
var grid = this.AssociatedObject;
int rowHandle = grid.FindRowByValue("", obj);
this.WorkOutCell(rowHandle);
}
}
protected void CellValueChanging(object sender, CellValueChangedEventArgs e) {
var grid = this.AssociatedObject;
grid.View.PostEditor();
}
protected void GridCellValueChanged(object sender, CellValueChangedEventArgs e) {
if(e.Column.FieldName == this.CheckableProperty && !this._LocalSet) {
this._LocalSet = true;
this.WorkOutCell(e.RowHandle);
this._LocalSet = false;
}
}
protected void WorkOutCell(int rowHandle) {
var grid = this.AssociatedObject;
int groupRowHandle = grid.GetParentRowHandle(rowHandle);
if(grid.IsValidRowHandle(groupRowHandle) && grid.IsGroupRowHandle(groupRowHandle)) {
bool? value = this.GetRowChildrenState(groupRowHandle);
var dic = new Dictionary<int, bool?>(this.GetUpdatedDic(groupRowHandle, value));
grid.GetRowState(groupRowHandle, true).SetCurrentValue(CheckableGroupBehavior.CheckListProperty, dic);
this.WorkOutCell(groupRowHandle);
}
}
protected bool? GetRowChildrenState(int rowHandle) {
var grid = this.AssociatedObject;
bool? result = null, b;
if(grid.IsValidRowHandle(rowHandle) && grid.IsGroupRowHandle(rowHandle)) {
int count = grid.GetChildRowCount(rowHandle);
int i = -1;
while(++i < count) {
var childHandle = grid.GetChildRowHandle(rowHandle, i);
int childLevel = grid.GetRowLevelByRowHandle(childHandle);
if(grid.IsGroupRowHandle(childHandle)) {
var dic = (Dictionary<int, bool?>)grid.GetRowState(childHandle, true).GetValue(CheckableGroupBehavior.CheckListProperty);
b = dic[childLevel];
} else {
var column = grid.Columns.Where(c => c.FieldName == this.CheckableProperty).FirstOrDefault();
if(column != null) {
b = (bool)grid.GetCellValue(childHandle, column);
} else {
b = false;
}
}
if(b.HasValue) {
if(result.HasValue) {
if(result.Value ^ b.Value) {
return null;
}
} else {
result = b;
}
} else {
return null;
}
}
}
return result;
}
protected Dictionary<int, bool?> GetUpdatedDic(int rowHandle, bool? value) {
Dictionary<int, bool?> result = null;
if(this.AssociatedObject.IsValidRowHandle(rowHandle)){
int groupLevel = this.AssociatedObject.GetRowLevelByRowHandle(rowHandle);
var rowState = this.AssociatedObject.GetRowState(rowHandle, true);
result = (Dictionary<int, bool?>)rowState.GetValue(CheckableGroupBehavior.CheckListProperty);
result[groupLevel] = value;
}
return result;
}
protected void WorkOutGroup(int rowHandle, bool? value) {
var grid = this.AssociatedObject;
var parentRowState = grid.GetRowState(rowHandle, true);
if(grid.IsValidRowHandle(rowHandle) && grid.IsGroupRowHandle(rowHandle)) {
int count = grid.GetChildRowCount(rowHandle);
int i = -1;
while(++i < count) {
var childHandle = grid.GetChildRowHandle(rowHandle, i);
if(grid.IsGroupRowHandle(childHandle)) {
var rowState = this.AssociatedObject.GetRowState(childHandle, true);
rowState.SetCurrentValue(CheckableGroupBehavior.CheckListProperty, new Dictionary<int, bool?>(this.GetUpdatedDic(childHandle, value)));
this.WorkOutGroup(childHandle, value);
} else {
var column = grid.Columns.Where(c => c.FieldName == this.CheckableProperty).FirstOrDefault();
if(column != null) {
grid.SetCellValue(childHandle, column, value);
}
}
}
this.WorkOutCell(rowHandle);
}
}
#region Template For GroupRowValue
protected DataTemplate GetGroupValueTemplate() {
string xamlDataTemplate = "<DataTemplate>";
xamlDataTemplate += "<self:GroupCheckBox Content=\"{Binding Value}\" Margin=\"8,0,0,0\" CheckStates=\"{Binding Path=RowData.RowState.(self:CheckableGroupBehavior.CheckList), Mode=TwoWay}\"/>";
xamlDataTemplate += "</DataTemplate>";
ParserContext context = new ParserContext();
context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
context.XmlnsDictionary.Add("self", "clr-namespace:DevExpress.Example03;assembly=DevExpress.Example03");
DataTemplate template = (DataTemplate)XamlReader.Parse(xamlDataTemplate, context);
return template;
}
#endregion Template for GroupRowValue
}
}