-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathTableViewColumn.cs
More file actions
565 lines (495 loc) · 20.4 KB
/
TableViewColumn.cs
File metadata and controls
565 lines (495 loc) · 20.4 KB
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
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
using System;
using System.Collections.Generic;
using WinUI.TableView.Extensions;
using SD = WinUI.TableView.SortDirection;
namespace WinUI.TableView;
/// <summary>
/// Represents a column in a TableView.
/// </summary>
[StyleTypedProperty(Property = nameof(HeaderStyle), StyleTargetType = typeof(TableViewColumnHeader))]
[StyleTypedProperty(Property = nameof(CellStyle), StyleTargetType = typeof(TableViewCell))]
public abstract partial class TableViewColumn : DependencyObject
{
private TableViewColumnHeader? _headerControl;
private double _desiredWidth;
private SD? _sortDirection;
private bool _isFiltered;
private bool _isFrozen;
private Func<object, object?>? _funcCompiledPropertyPath;
/// <summary>
/// Initializes a new instance of the <see cref="TableViewColumn"/> class with default conditional cell styles.
/// </summary>
public TableViewColumn()
{
SetValue(ConditionalCellStylesProperty, new TableViewConditionalCellStylesCollection());
}
/// <summary>
/// Generates a display element for the cell.
/// </summary>
/// <param name="cell">The cell for which the element is generated.</param>
/// <param name="dataItem">The data item associated with the cell.</param>
/// <returns>A FrameworkElement representing the display element.</returns>
public abstract FrameworkElement GenerateElement(TableViewCell cell, object? dataItem);
/// <summary>
/// Generates an editing element for the cell.
/// </summary>
/// <param name="cell">The cell for which the editing element is generated.</param>
/// <param name="dataItem">The data item associated with the cell.</param>
/// <returns>A FrameworkElement representing the editing element.</returns>
public abstract FrameworkElement GenerateEditingElement(TableViewCell cell, object? dataItem);
/// <summary>
/// Refreshes the display element for the cell.
/// </summary>
/// <param name="cell">The cell for which the element is refreshed.</param>
/// <param name="dataItem">The data item associated with the cell.</param>
public virtual void RefreshElement(TableViewCell cell, object? dataItem) { }
/// <summary>
/// Called to prepare the cell for editing.
/// </summary>
/// <param name="cell">The cell to prepare for editing.</param>
/// <param name="routedEvent">The routed event.</param>
/// <returns>Should return the unedited cell value.</returns>
protected internal virtual object? PrepareCellForEdit(TableViewCell cell, RoutedEventArgs routedEvent)
{
return default;
}
/// <summary>
/// Called to end the editing session for the cell.
/// </summary>
/// <param name="cell">The cell whose editing session is being ended.</param>
/// <param name="dataItem">The data item associated with the cell.</param>
/// <param name="editAction">The edit action.</param>
/// <param name="uneditedValue">The unedited value of the cell.</param>
protected internal virtual void EndCellEditing(TableViewCell cell, object? dataItem, TableViewEditAction editAction, object? uneditedValue)
{
// Nothing to do here by default. Derived classes can override to commit changes if needed.
}
/// <summary>
/// Updates the state of the element for the cell.
/// </summary>
/// <param name="cell">The cell for which the element state is updated.</param>
/// <param name="dataItem">The data item associated with the cell.</param>
public virtual void UpdateElementState(TableViewCell cell, object? dataItem) { }
/// <summary>
/// Sets the owning collection for the column.
/// </summary>
/// <param name="collection">The owning collection.</param>
internal void SetOwningCollection(TableViewColumnsCollection collection)
{
OwningCollection = collection;
}
/// <summary>
/// Sets the owning TableView for the column.
/// </summary>
/// <param name="tableView">The owning TableView.</param>
internal void SetOwningTableView(TableView tableView)
{
TableView = tableView;
}
/// <summary>
/// Gets the content of the cell for the specified data item.
/// </summary>
/// <param name="dataItem">The data item.</param>
/// <returns>The content of the cell.</returns>
public virtual object? GetCellContent(object? dataItem)
{
return default;
}
/// <summary>
/// Gets the clipboard content of the cell for the specified data item.
/// </summary>
/// <param name="dataItem">The data item.</param>
/// <returns>The clipboard content of the cell.</returns>
public virtual object? GetClipboardContent(object? dataItem)
{
if (dataItem is null)
return null;
if (_funcCompiledPropertyPath is null && !string.IsNullOrWhiteSpace(ClipboardContentBindingPropertyPath))
_funcCompiledPropertyPath = dataItem.GetFuncCompiledPropertyPath(ClipboardContentBindingPropertyPath!);
if (_funcCompiledPropertyPath is not null)
dataItem = _funcCompiledPropertyPath(dataItem);
if (ClipboardContentBinding?.Converter is not null)
{
dataItem = ClipboardContentBinding.Converter.Convert(
dataItem,
typeof(object),
ClipboardContentBinding.ConverterParameter,
ClipboardContentBinding.ConverterLanguage);
}
return dataItem;
}
/// <summary>
/// Gets or sets the header content of the column.
/// </summary>
public object Header
{
get => GetValue(HeaderProperty);
set => SetValue(HeaderProperty, value);
}
/// <summary>
/// Gets or sets the width of the column.
/// </summary>
public GridLength Width
{
get => (GridLength)GetValue(WidthProperty);
set => SetValue(WidthProperty, value);
}
/// <summary>
/// Gets or sets the minimum width of the column.
/// </summary>
public double? MinWidth
{
get => (double?)GetValue(MinWidthProperty);
set => SetValue(MinWidthProperty, value);
}
/// <summary>
/// Gets or sets the maximum width of the column.
/// </summary>
public double? MaxWidth
{
get => (double?)GetValue(MaxWidthProperty);
set => SetValue(MaxWidthProperty, value);
}
/// <summary>
/// Gets or sets the actual width of the column.
/// </summary>
public double ActualWidth
{
get => (double)GetValue(ActualWidthProperty);
set => SetValue(ActualWidthProperty, value);
}
/// <summary>
/// Gets or sets the ColumnAutoWidthMode of the column.
/// </summary>
public ColumnAutoWidthMode? ColumnAutoWidthMode
{
get => (ColumnAutoWidthMode?)GetValue(ColumnAutoWidthModeProperty);
set => SetValue(ColumnAutoWidthModeProperty, value);
}
/// <summary>
/// Gets or sets a value indicating whether the column can be resized.
/// </summary>
public bool CanResize
{
get => (bool)GetValue(CanResizeProperty);
set => SetValue(CanResizeProperty, value);
}
/// <summary>
/// Gets or sets a value indicating whether the column is read-only.
/// </summary>
public bool IsReadOnly
{
get => (bool)GetValue(IsReadOnlyProperty);
set => SetValue(IsReadOnlyProperty, value);
}
/// <summary>
/// Gets or sets the style that is used when rendering the column header.
/// </summary>
public Style? HeaderStyle
{
get => (Style?)GetValue(HeaderStyleProperty);
set => SetValue(HeaderStyleProperty, value);
}
/// <summary>
/// Gets or sets the style that is used to render cells in the column.
/// </summary>
public Style CellStyle
{
get => (Style)GetValue(CellStyleProperty);
set => SetValue(CellStyleProperty, value);
}
/// <summary>
/// Gets or sets the header control for the column.
/// </summary>
public TableViewColumnHeader? HeaderControl
{
get => _headerControl;
internal set
{
_headerControl = value;
EnsureHeaderStyle();
}
}
/// <summary>
/// Gets or sets the visibility of the column.
/// </summary>
public Visibility Visibility
{
get => (Visibility)GetValue(VisibilityProperty);
set => SetValue(VisibilityProperty, value);
}
/// <summary>
/// Gets or sets a custom tag object for the column.
/// </summary>
public object Tag
{
get => GetValue(TagProperty);
set => SetValue(TagProperty, value);
}
/// <summary>
/// Gets or sets a value indicating whether the column can be sorted. This can be overridden by the TableView.
/// </summary>
public bool CanSort
{
get => (bool)GetValue(CanSortProperty);
set => SetValue(CanSortProperty, value);
}
/// <summary>
/// Gets or sets a value indicating whether the column can be filtered.
/// </summary>
public bool CanFilter
{
get => (bool)GetValue(CanFilterProperty);
set => SetValue(CanFilterProperty, value);
}
/// <summary>
/// Gets or sets the order in which this column should be displayed.
/// </summary>
public int? Order
{
get => (int?)GetValue(OrderProperty);
set => SetValue(OrderProperty, value);
}
/// <summary>
/// Gets or sets a value indicating whether the column can be reordered.
/// </summary>
public bool CanReorder
{
get => (bool)GetValue(CanReorderProperty);
set => SetValue(CanReorderProperty, value);
}
/// <summary>
/// Gets or sets the collection of conditional cell styles for the column.
/// </summary>
public IList<TableViewConditionalCellStyle> ConditionalCellStyles
{
get => (IList<TableViewConditionalCellStyle>)GetValue(ConditionalCellStylesProperty);
set => SetValue(ConditionalCellStylesProperty, value);
}
internal TableViewColumnsCollection? OwningCollection { get; set; }
/// <summary>
/// Gets or sets the desired width of the column.
/// </summary>
internal double DesiredWidth
{
get => _desiredWidth;
set
{
if (_desiredWidth != value)
{
_desiredWidth = value;
OwningCollection?.HandleColumnPropertyChanged(this, nameof(DesiredWidth));
}
}
}
/// <summary>
/// Gets or sets a value indicating whether the column is auto-generated.
/// </summary>
public bool IsAutoGenerated { get; internal set; }
/// <summary>
/// Gets or sets a value indicating whether the column uses a single element for display and editing.
/// </summary>
public bool UseSingleElement { get; set; }
/// <summary>
/// Gets or sets the sort direction for the column.
/// </summary>
public SD? SortDirection
{
get => _sortDirection;
set
{
_sortDirection = value;
OnSortDirectionChanged();
}
}
/// <summary>
/// Gets or sets a value indicating whether the column is filtered.
/// </summary>
public bool IsFiltered
{
get => _isFiltered;
set
{
_isFiltered = value;
OnIsFilteredChanged();
}
}
/// <summary>
/// Gets a value indicating whether the column can be horizontally scrolled.
/// </summary>
public bool IsFrozen
{
get => _isFrozen;
internal set
{
if (_isFrozen != value)
{
_isFrozen = value;
OwningCollection?.HandleColumnPropertyChanged(this, nameof(IsFrozen));
}
}
}
/// <summary>
/// Gets or sets the data binding used to retrieve cell content when copying to the clipboard.
/// </summary>
public virtual Binding? ClipboardContentBinding { get; set; }
/// <summary>
/// Gets the property path for the <see cref="ClipboardContentBinding"/>.
/// </summary>
internal string? ClipboardContentBindingPropertyPath => ClipboardContentBinding?.Path?.Path;
/// <summary>
/// Gets the owning TableView for the column.
/// </summary>
protected internal TableView? TableView { get; private set; }
/// <summary>
/// Handles changes to the SortDirection property.
/// </summary>
private void OnSortDirectionChanged()
{
HeaderControl?.OnSortDirectionChanged();
}
/// <summary>
/// Handles changes to the IsFiltered property.
/// </summary>
private void OnIsFilteredChanged()
{
HeaderControl?.OnIsFilteredChanged();
}
/// <summary>
/// Ensures the header style is applied to the header control.
/// </summary>
internal void EnsureHeaderStyle()
{
if (_headerControl is not null)
{
_headerControl.Style = HeaderStyle ?? TableView?.ColumnHeaderStyle;
}
}
/// <summary>
/// Handles changes to the Width property.
/// </summary>
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableViewColumn { OwningCollection: { } } column)
{
if (e.Property == CellStyleProperty)
column.OwningCollection.HandleColumnPropertyChanged(column, nameof(CellStyle));
else if (e.Property == WidthProperty)
column.OwningCollection.HandleColumnPropertyChanged(column, nameof(Width));
else if (e.Property == MinWidthProperty)
column.OwningCollection.HandleColumnPropertyChanged(column, nameof(MinWidth));
else if (e.Property == MaxWidthProperty)
column.OwningCollection.HandleColumnPropertyChanged(column, nameof(MaxWidth));
else if (e.Property == ActualWidthProperty)
column.OwningCollection.HandleColumnPropertyChanged(column, nameof(ActualWidth));
else if (e.Property == IsReadOnlyProperty)
column.OwningCollection.HandleColumnPropertyChanged(column, nameof(IsReadOnly));
else if (e.Property == VisibilityProperty)
column.OwningCollection.HandleColumnPropertyChanged(column, nameof(Visibility));
else if (e.Property == OrderProperty)
column.OwningCollection.HandleColumnPropertyChanged(column, nameof(Order));
}
}
/// <summary>
/// Handles changes to the CanFilter property.
/// </summary>
private static void OnCanFilterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableViewColumn column && column.HeaderControl is not null)
{
column.HeaderControl.SetFilterButtonVisibility();
}
}
/// <summary>
/// Handles changes to the HeaderStyle property.
/// </summary>
private static void OnHeaderStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableViewColumn column)
{
column.EnsureHeaderStyle();
}
}
/// <summary>
/// Gets or sets the member path to use for sorting instead of the default binding path.
/// </summary>
public string? SortMemberPath
{
get => (string?)GetValue(SortMemberPathProperty);
set => SetValue(SortMemberPathProperty, value);
}
/// <summary>
/// Identifies the SortMemberPath dependency property.
/// </summary>
public static readonly DependencyProperty SortMemberPathProperty = DependencyProperty.Register(nameof(SortMemberPath), typeof(string), typeof(TableViewColumn), new PropertyMetadata(null));
/// <summary>
/// Identifies the HeaderStyle dependency property.
/// </summary>
public static readonly DependencyProperty HeaderStyleProperty = DependencyProperty.Register(nameof(HeaderStyle), typeof(Style), typeof(TableViewColumn), new PropertyMetadata(null, OnHeaderStyleChanged));
/// <summary>
/// Identifies the CellStyle dependency property.
/// </summary>
public static readonly DependencyProperty CellStyleProperty = DependencyProperty.Register(nameof(CellStyle), typeof(Style), typeof(TableViewColumn), new PropertyMetadata(null, OnPropertyChanged));
/// <summary>
/// Identifies the Header dependency property.
/// </summary>
public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(nameof(Header), typeof(object), typeof(TableViewColumn), new PropertyMetadata(null));
/// <summary>
/// Identifies the Width dependency property.
/// </summary>
public static readonly DependencyProperty WidthProperty = DependencyProperty.Register(nameof(Width), typeof(GridLength), typeof(TableViewColumn), new PropertyMetadata(GridLength.Auto, OnPropertyChanged));
/// <summary>
/// Identifies the MinWidth dependency property.
/// </summary>
public static readonly DependencyProperty MinWidthProperty = DependencyProperty.Register(nameof(MinWidth), typeof(double?), typeof(TableViewColumn), new PropertyMetadata(default, OnPropertyChanged));
/// <summary>
/// Identifies the MaxWidth dependency property.
/// </summary>
public static readonly DependencyProperty MaxWidthProperty = DependencyProperty.Register(nameof(MaxWidth), typeof(double?), typeof(TableViewColumn), new PropertyMetadata(default, OnPropertyChanged));
/// <summary>
/// Identifies the ActualWidth dependency property.
/// </summary>
public static readonly DependencyProperty ActualWidthProperty = DependencyProperty.Register(nameof(ActualWidth), typeof(double), typeof(TableViewColumn), new PropertyMetadata(0d, OnPropertyChanged));
/// <summary>
/// Identifies the ActualWidth dependency property.
/// </summary>
public static readonly DependencyProperty ColumnAutoWidthModeProperty = DependencyProperty.Register(nameof(ColumnAutoWidthMode), typeof(ColumnAutoWidthMode?), typeof(TableViewColumn), new PropertyMetadata(null));
/// <summary>
/// Identifies the CanResize dependency property.
/// </summary>
public static readonly DependencyProperty CanResizeProperty = DependencyProperty.Register(nameof(CanResize), typeof(bool), typeof(TableViewColumn), new PropertyMetadata(true));
/// <summary>
/// Identifies the IsReadOnly dependency property.
/// </summary>
public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(nameof(IsReadOnly), typeof(bool), typeof(TableViewColumn), new PropertyMetadata(false, OnPropertyChanged));
/// <summary>
/// Identifies the Visibility dependency property.
/// </summary>
public static readonly DependencyProperty VisibilityProperty = DependencyProperty.Register(nameof(Visibility), typeof(Visibility), typeof(TableViewColumn), new PropertyMetadata(Visibility.Visible, OnPropertyChanged));
/// <summary>
/// Identifies the Tag dependency property.
/// </summary>
public static readonly DependencyProperty TagProperty = DependencyProperty.Register(nameof(Tag), typeof(object), typeof(TableViewColumn), new PropertyMetadata(null));
/// <summary>
/// Identifies the CanSort dependency property.
/// </summary>
public static readonly DependencyProperty CanSortProperty = DependencyProperty.Register(nameof(CanSort), typeof(bool), typeof(TableViewColumn), new PropertyMetadata(true));
/// <summary>
/// Identifies the CanFilter dependency property.
/// </summary>
public static readonly DependencyProperty CanFilterProperty = DependencyProperty.Register(nameof(CanFilter), typeof(bool), typeof(TableViewColumn), new PropertyMetadata(true, OnCanFilterChanged));
/// <summary>
/// Identifies the Order dependency property.
/// </summary>
public static readonly DependencyProperty OrderProperty = DependencyProperty.Register(nameof(Order), typeof(int?), typeof(TableViewColumn), new PropertyMetadata(null, OnPropertyChanged));
/// <summary>
/// Identifies the CanReorder dependency property.
/// </summary>
public static readonly DependencyProperty CanReorderProperty = DependencyProperty.Register(nameof(CanReorder), typeof(bool), typeof(TableViewColumn), new PropertyMetadata(true));
/// <summary>
/// Identifies the <see cref="ConditionalCellStyles"/> dependency property.
/// </summary>
public static readonly DependencyProperty ConditionalCellStylesProperty = DependencyProperty.Register(nameof(ConditionalCellStyles), typeof(IList<TableViewConditionalCellStyle>), typeof(TableViewColumn), new PropertyMetadata(default));
}