-
Notifications
You must be signed in to change notification settings - Fork 4
/
Band.cs
703 lines (587 loc) · 18.1 KB
/
Band.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media.Animation;
using System.Windows.Media.TextFormatting;
using KevinComponent.Assist;
namespace KevinComponent
{
public abstract class Band : FrameworkElement
{
static Band()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Band), new FrameworkPropertyMetadata(typeof(Band)));
WidthProperty.OverrideMetadata(typeof(Band), new FrameworkPropertyMetadata(20d));
MinWidthProperty.OverrideMetadata(typeof(Band), new FrameworkPropertyMetadata(20d));
}
protected Band()
{
SyncDataGridColumn = new FlexGridColumn(this);
Bands = new BandCollection(this);
Bands.CollectionChanged += OnBandsCollectionChanged;
Bands.VirtualBandItemsSourceChanged += OnVirtualBandItemsSourceChanged;
Bands.VirtualBandItemsSourceCollectionChanged += OnVirtualBandItemsSourceCollectionChanged;
UpdateDefaultStyle();
}
protected Band(VirtualBand virtualBand) : this()
{
OwnerFlexGrid = virtualBand.OwnerFlexGrid;
ParentBand = virtualBand.ParentBand;
CellStyle = virtualBand.CellStyle;
MinWidth = virtualBand.MinWidth;
MaxWidth = virtualBand.MaxWidth;
Width = virtualBand.Width;
HorizontalHeaderAlignment = virtualBand.HorizontalHeaderAlignment;
VerticalHeaderAlignment = virtualBand.VerticalHeaderAlignment;
DerivationFrom = virtualBand;
if (virtualBand.HeaderBinding != null)
BindingOperations.SetBinding(this, HeaderProperty, virtualBand.HeaderBinding);
else
Header = virtualBand.Header;
}
#region Dependency Properties
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register(
"Header",
typeof(object),
typeof(Band),
new FrameworkPropertyMetadata(null, OnHeaderPropertyChanged));
public static readonly DependencyProperty HorizontalHeaderAlignmentProperty =
DependencyProperty.Register(
"HorizontalHeaderAlignment",
typeof(HorizontalAlignment),
typeof(Band),
new FrameworkPropertyMetadata(HorizontalAlignment.Center));
public static readonly DependencyProperty VerticalHeaderAlignmentProperty =
DependencyProperty.Register(
"VerticalHeaderAlignment",
typeof(VerticalAlignment),
typeof(Band),
new FrameworkPropertyMetadata(VerticalAlignment.Center));
public static readonly DependencyProperty CellTemplateProperty =
DependencyProperty.Register(
"CellTemplate",
typeof(DataTemplate),
typeof(Band),
new FrameworkPropertyMetadata(null, OnCellTemplatePropertyChanged));
public static readonly DependencyProperty CellEditingTemplateProperty =
DependencyProperty.Register(
"CellEditingTemplate",
typeof(DataTemplate),
typeof(Band),
new FrameworkPropertyMetadata(null, OnCellEditingTemplatePropertyChanged));
public static readonly DependencyProperty CellStyleProperty =
DependencyProperty.Register(
"CellStyle",
typeof(Style),
typeof(Band),
new FrameworkPropertyMetadata(null, OnCellStylePropertyChanged));
public static readonly DependencyProperty IsReadOnlyProperty =
DependencyProperty.Register(
"IsReadOnly",
typeof(bool),
typeof(Band),
new FrameworkPropertyMetadata(false));
public static readonly DependencyProperty CanUserSortProperty =
DependencyProperty.Register(
"CanUserSort",
typeof(bool),
typeof(Band),
new FrameworkPropertyMetadata(false));
public static readonly DependencyProperty SortDirectionProperty =
DependencyProperty.Register(
"SortDirection",
typeof(ListSortDirection?),
typeof(Band),
new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty SortMemberPathProperty =
DependencyProperty.Register(
"SortMemberPath",
typeof(string),
typeof(Band),
new FrameworkPropertyMetadata(string.Empty));
private static void OnHeaderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var band = d as Band;
if (band?.BandHeader != null)
{
bool useHeaderTemplate = false;
var newValue = e.NewValue;
if (newValue is DataTemplate dt)
{
var cp = new ContentPresenter();
cp.ContentTemplate = dt;
BindingOperations.SetBinding(cp, ContentPresenter.ContentProperty, new Binding());
newValue = cp;
useHeaderTemplate = true;
}
band.BandHeader.Content = newValue;
band._useHeaderTemplate = useHeaderTemplate;
}
}
private static void OnCellTemplatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var band = d as Band;
band?.OnCellTemplateChanged((DataTemplate)e.OldValue, (DataTemplate)e.NewValue);
}
private static void OnCellEditingTemplatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var band = d as Band;
band?.OnCellEditingTemplateChanged((DataTemplate)e.OldValue, (DataTemplate)e.NewValue);
}
private static void OnCellStylePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var band = d as Band;
band?.OnCellStyleChanged((Style)e.OldValue, (Style)e.NewValue);
}
#endregion
#region Private Variables
BandHeader? _bandHeader;
bool _useHeaderTemplate;
FlexGrid? _ownerFlexGrid;
bool _settingWithoutParentBand;
bool _settingWithoutSubBands;
Band? _parentBand;
#endregion
#region Internal Properties
internal VirtualBand? DerivationFrom { get; }
internal Band? ParentBand
{
get => _parentBand;
set
{
if (_parentBand != value)
{
var oldParentBand = _parentBand;
_parentBand = value;
OnParentBandChanged(oldParentBand, value);
}
}
}
internal FlexGrid? OwnerFlexGrid
{
get => _ownerFlexGrid;
set
{
if (_ownerFlexGrid != value)
{
var oldOwnerFlexGrid = _ownerFlexGrid;
_ownerFlexGrid = value;
OnOwnerFlexGridChanged(oldOwnerFlexGrid, value);
}
}
}
internal BandHeader BandHeader
{
get
{
if (_bandHeader == null)
{
_bandHeader = new BandHeader(this);
_bandHeader.Width = Width;
}
return _bandHeader;
}
}
internal FlexGridColumn SyncDataGridColumn { get; }
#endregion
#region Public Properties
public int Depth
=> Bands.MaxDepth + 1;
public BandCollection Bands { get; }
public bool HasChildBands
=> Bands.Count > 0;
public object Header
{
get => GetValue(HeaderProperty);
set => SetValue(HeaderProperty, value);
}
public HorizontalAlignment HorizontalHeaderAlignment
{
get => (HorizontalAlignment)GetValue(HorizontalHeaderAlignmentProperty);
set => SetValue(HorizontalHeaderAlignmentProperty, value);
}
public VerticalAlignment VerticalHeaderAlignment
{
get => (VerticalAlignment)GetValue(VerticalHeaderAlignmentProperty);
set => SetValue(VerticalHeaderAlignmentProperty, value);
}
public DataTemplate CellTemplate
{
get => (DataTemplate)GetValue(CellTemplateProperty);
set => SetValue(CellTemplateProperty, value);
}
public DataTemplate CellEditingTemplate
{
get => (DataTemplate)GetValue(CellEditingTemplateProperty);
set => SetValue(CellEditingTemplateProperty, value);
}
public Style CellStyle
{
get => (Style)GetValue(StyleProperty);
set => SetValue(StyleProperty, value);
}
public bool IsReadOnly
{
get => (bool)GetValue(IsReadOnlyProperty);
set => SetValue(IsReadOnlyProperty, value);
}
public bool CanUserSort
{
get => (bool)GetValue(CanUserSortProperty);
set => SetValue(CanUserSortProperty, value);
}
public ListSortDirection? SortDirection
{
get => (ListSortDirection?)GetValue(SortDirectionProperty);
set => SetValue(SortDirectionProperty, value);
}
public string SortMemberPath
{
get => (string)GetValue(SortMemberPathProperty);
set => SetValue(SortMemberPathProperty, value);
}
#endregion
#region Private Methods
private void SetBandsOwnerFlexGrid(FlexGrid? flexGrid)
{
Bands.OwnerFlexGrid = flexGrid;
}
private DataTemplate GetTemplate(bool isEditing)
{
if (isEditing && !IsReadOnly && CellEditingTemplate != null)
return CellEditingTemplate;
else
return CellTemplate;
}
private void AttachEventHandlers(ContentPresenter contentPresenter, bool isEditing)
{
DetachEventHandlers(contentPresenter, isEditing);
if (isEditing && !IsReadOnly)
{
contentPresenter.Loaded += OnCellEditingTemplateLoaded;
contentPresenter.Unloaded += OnCellEditingTemplateUnloaded;
}
else
{
contentPresenter.Loaded += OnCellTemplateLoaded;
contentPresenter.Unloaded += OnCellTemplateUnloaded;
}
}
private void DetachEventHandlers(ContentPresenter contentPresenter, bool isEditing)
{
if (isEditing && !IsReadOnly)
{
contentPresenter.Loaded -= OnCellEditingTemplateLoaded;
contentPresenter.Unloaded -= OnCellEditingTemplateUnloaded;
}
else
{
contentPresenter.Loaded -= OnCellTemplateLoaded;
contentPresenter.Unloaded -= OnCellTemplateUnloaded;
}
}
private double LimitWidthForPreventOverflow(ref double totalWidth, double newWidth)
{
totalWidth -= newWidth;
if (totalWidth < 0)
{
var result = newWidth + totalWidth;
totalWidth = 0;
return result;
}
return newWidth;
}
private void SetSubBandsWidth(double newTotalWidth)
{
if (_settingWithoutSubBands)
return;
var unusedWidth = newTotalWidth;
var prevTotalWidth = Bands.TotalWidth;
foreach (var band in Bands)
{
if (band is VirtualBand vband)
{
foreach (var subBand in vband.VirtualizedBands)
{
var calcWidth = Math.Max(subBand.MinWidth, newTotalWidth * (subBand.Width / prevTotalWidth));
calcWidth = LimitWidthForPreventOverflow(ref unusedWidth, calcWidth);
subBand.SetWidthWithoutParentBand(calcWidth);
}
}
else
{
var calcWidth = Math.Max(band.MinWidth, newTotalWidth * (band.Width / prevTotalWidth));
calcWidth = LimitWidthForPreventOverflow(ref unusedWidth, calcWidth);
band.SetWidthWithoutParentBand(calcWidth);
}
}
}
private void SetParentBandWidth(double addedWidth)
{
if (ParentBand == null || _settingWithoutParentBand)
return;
var newWidth = ParentBand.Width + addedWidth;
newWidth = Math.Max(MinWidth, newWidth);
newWidth = Math.Min(MaxWidth, newWidth);
ParentBand.SetWidthWithoutSubBands(newWidth);
}
private void SetWidthWithoutParentBand(double width)
{
_settingWithoutParentBand = true;
Width = width;
_settingWithoutParentBand = false;
}
private void SetWidthWithoutSubBands(double width)
{
// Lock Setting SubBands.
_settingWithoutSubBands = true;
Width = width;
// Unlock Setting SubBands.
_settingWithoutSubBands = false;
}
private void UpdateAllWidths()
{
double totalWidth = 0d;
double totalMinWidth = 0d;
double totalMaxWidth = 0d;
foreach (var band in Bands)
{
if (band is VirtualBand vband)
{
foreach (var subBand in vband.VirtualizedBands)
{
totalWidth += subBand.Width;
totalMinWidth += subBand.MinWidth;
totalMaxWidth += subBand.MaxWidth;
}
}
else
{
totalWidth += band.Width;
totalMinWidth += band.MinWidth;
totalMaxWidth += band.MaxWidth;
}
}
MinWidth = totalMinWidth;
MaxWidth = totalMaxWidth;
Width = totalWidth;
ParentBand?.UpdateAllWidths();
}
private void SetBindingSource(BindingBase bindingBase, object? source)
{
if (bindingBase is Binding newBinding)
{
if (newBinding.Source == null
&& newBinding.ElementName == null
&& newBinding.RelativeSource == null)
newBinding.Source = source;
}
else if (bindingBase is MultiBinding newMultiBinding)
{
foreach (var child in newMultiBinding.Bindings)
SetBindingSource(child, source);
}
else if (bindingBase is PriorityBinding newPrioBinding)
{
foreach (var child in newPrioBinding.Bindings)
SetBindingSource(child, source);
}
}
private void UpdateVirtualBandBindings(ContentPresenter cp)
{
if (DerivationFrom == null && DataContext == null)
return;
var propertyAndBindings = Utils.GetBindings<IVirtualBandBindable>(cp);
foreach (var (element, property, bindingBase) in propertyAndBindings)
{
var cell = FlexGridAssist.GetParentCell(cp);
if (cell == null)
continue;
if (Utils.GetIndexerValue(cell.DataContext, new object[] { DataContext }, out object? bindingSource))
{
var newBindingBase = Utils.CloneBinding((BindingBase)bindingBase);
SetBindingSource(newBindingBase, bindingSource);
BindingOperations.SetBinding(element, property, newBindingBase);
}
else
{
var newBindingBase = Utils.CloneBinding((BindingBase)bindingBase);
SetBindingSource(newBindingBase, null);
BindingOperations.SetBinding(element, property, newBindingBase);
}
}
}
private void SetWidth(double oldValue, double newValue)
{
if (this is VirtualBand)
return;
newValue = Math.Max(MinWidth, newValue);
newValue = Math.Min(MaxWidth, newValue);
BandHeader.Width = newValue;
SetSubBandsWidth(newValue);
SetParentBandWidth(newValue - oldValue);
}
private void SetSyncDataGridColumnMinWidth(double newValue)
{
if (SyncDataGridColumn != null)
SyncDataGridColumn.MinWidth = newValue;
}
private void SetSyncDataGridColumnMaxWidth(double newValue)
{
if (SyncDataGridColumn != null)
SyncDataGridColumn.MaxWidth = newValue;
}
private void SetSyncDataGridColumnCanUserSort(bool newValue)
{
if (SyncDataGridColumn != null)
SyncDataGridColumn.CanUserSort = newValue;
}
private void SetSyncDataGridColumnSortDirection(ListSortDirection? newValue)
{
if (SyncDataGridColumn != null)
SyncDataGridColumn.SortDirection = newValue;
if (BandHeader != null)
BandHeader.SortDirection = newValue;
}
private void SetSyncDataGridColumnSortMemberPath(string newValue)
{
if (SyncDataGridColumn != null)
SyncDataGridColumn.SortMemberPath = newValue;
}
private void SetBandHeaderDataContext(object newValue)
{
if (BandHeader != null)
BandHeader.DataContext = newValue;
}
#endregion
#region Private EventHandlers
private void OnVirtualBandItemsSourceCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
UpdateAllWidths();
}
private void OnVirtualBandItemsSourceChanged(object sender, DependencyPropertyChangedEventArgs e)
{
UpdateAllWidths();
}
private void OnBandsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
UpdateAllWidths();
}
private void OnCellTemplateLoaded(object sender, RoutedEventArgs e)
{
if (sender is ContentPresenter cp)
{
OnCellTemplateLoaded(cp);
UpdateVirtualBandBindings(cp);
}
}
private void OnCellTemplateUnloaded(object sender, RoutedEventArgs e)
{
if (sender is ContentPresenter cp)
{
cp.Loaded -= OnCellTemplateLoaded;
cp.Unloaded -= OnCellTemplateUnloaded;
}
}
private void OnCellEditingTemplateLoaded(object sender, RoutedEventArgs e)
{
if (sender is ContentPresenter cp)
{
OnCellEditingTemplateLoaded(cp);
UpdateVirtualBandBindings(cp);
PrepareEdit(cp);
}
}
private void OnCellEditingTemplateUnloaded(object sender, RoutedEventArgs e)
{
if (sender is ContentPresenter cp)
{
cp.Loaded -= OnCellEditingTemplateLoaded;
cp.Unloaded -= OnCellEditingTemplateUnloaded;
}
}
#endregion
#region Internal Methods
internal FrameworkElement GenerateElement(DataGridCell cell, bool isEditing)
{
var cp = new ContentPresenter();
FlexGridAssist.SetParentCell(cp, cell);
BindingOperations.SetBinding(cp, ContentPresenter.ContentProperty, new Binding());
cp.ContentTemplate = GetTemplate(isEditing);
AttachEventHandlers(cp, isEditing);
return cp;
}
#endregion
#region Public Methods
public void PerformSort()
{
if (SyncDataGridColumn != null)
OwnerFlexGrid?.PerformSort(SyncDataGridColumn);
}
#endregion
#region Protected Virtual Methods
protected virtual void OnParentBandChanged(Band? oldParent, Band? newParent) { }
protected virtual void OnOwnerFlexGridChanged(FlexGrid? oldOwnerFlexGrid, FlexGrid? newOwnerFlexGrid)
{
SetBandsOwnerFlexGrid(newOwnerFlexGrid);
}
protected virtual void OnCellTemplateChanged(DataTemplate oldValue, DataTemplate newValue)
{
if (OwnerFlexGrid != null)
{
foreach (var item in OwnerFlexGrid.Items)
{
var row = (DataGridRow)OwnerFlexGrid.ItemContainerGenerator.ContainerFromItem(item);
if (row == null)
continue;
SyncDataGridColumn?.RefreshCellContent(row);
}
}
}
protected virtual void OnCellEditingTemplateChanged(DataTemplate oldValue, DataTemplate newValue) { }
protected virtual void OnCellStyleChanged(Style oldValue, Style newValue)
{
if (SyncDataGridColumn != null)
SyncDataGridColumn.CellStyle = newValue;
}
protected virtual void OnCellTemplateLoaded(ContentPresenter contentPresenter) { }
protected virtual void OnCellEditingTemplateLoaded(ContentPresenter contentPresenter)
{
OnCellTemplateLoaded(contentPresenter);
}
protected virtual void PrepareEdit(ContentPresenter contentPresenter) { }
#endregion
#region Protected Override Methods
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
// Width Sync.
if (e.Property == WidthProperty)
SetWidth((double)e.OldValue, (double)e.NewValue);
else if (e.Property == MinWidthProperty)
SetSyncDataGridColumnMinWidth((double)e.NewValue);
else if (e.Property == MaxWidthProperty)
SetSyncDataGridColumnMaxWidth((double)e.NewValue);
else if (e.Property == CanUserSortProperty)
SetSyncDataGridColumnCanUserSort((bool)e.NewValue);
else if (e.Property == SortDirectionProperty)
SetSyncDataGridColumnSortDirection((ListSortDirection?)e.NewValue);
else if (e.Property == SortMemberPathProperty)
SetSyncDataGridColumnSortMemberPath((string)e.NewValue);
else if (e.Property == DataContextProperty)
SetBandHeaderDataContext(e.NewValue);
}
#endregion
}
}