-
-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathTableViewDateColumn.cs
More file actions
263 lines (231 loc) · 9.87 KB
/
Copy pathTableViewDateColumn.cs
File metadata and controls
263 lines (231 loc) · 9.87 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
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using System;
using Windows.Globalization.DateTimeFormatting;
using Windows.System.UserProfile;
using WinUI.TableView.Controls;
using WinUI.TableView.Extensions;
using WinUI.TableView.Helpers;
using DayOfWeek = Windows.Globalization.DayOfWeek;
namespace WinUI.TableView;
/// <summary>
/// Represents a column in a TableView that displays a date.
/// </summary>
[StyleTypedProperty(Property = nameof(ElementStyle), StyleTargetType = typeof(TextBlock))]
[StyleTypedProperty(Property = nameof(EditingElementStyle), StyleTargetType = typeof(TableViewDatePicker))]
#if WINDOWS
[WinRT.GeneratedBindableCustomProperty]
#endif
public partial class TableViewDateColumn : TableViewBoundColumn
{
/// <summary>
/// Initializes a new instance of the TableViewDateColumn class.
/// </summary>
public TableViewDateColumn()
{
var languages = GlobalizationPreferences.Languages;
var patterns = languages.Count > 0 ? new DateTimeFormatter("shortdate", languages).Patterns : null;
DateFormat = patterns?.Count > 0 ? patterns[0] : "shortdate";
}
/// <summary>
/// Generates a TextBlock 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 TextBlock element.</returns>
public override FrameworkElement GenerateElement(TableViewCell cell, object? dataItem)
{
var textBlock = new TextBlock
{
Margin = new Thickness(12, 0, 12, 0),
};
textBlock.SetBinding(DateTimeFormatHelper.ValueProperty, Binding);
textBlock.SetBinding(DateTimeFormatHelper.FormatProperty, new Binding
{
Path = new PropertyPath(nameof(DateFormat)),
Source = this
});
return textBlock;
}
/// <summary>
/// Generates a TableViewDatePicker element for editing 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 TableViewDatePicker element.</returns>
public override FrameworkElement GenerateEditingElement(TableViewCell cell, object? dataItem)
{
var timePicker = new TableViewDatePicker
{
MinDate = MinDate,
MaxDate = MaxDate,
DateFormat = DateFormat,
IsTodayHighlighted = IsTodayHighlighted,
IsGroupLabelVisible = IsGroupLabelVisible,
IsOutOfScopeEnabled = IsOutOfScopeEnabled,
PlaceholderText = PlaceHolderText ?? TableViewLocalizedStrings.DatePickerPlaceholder,
DayOfWeekFormat = DayOfWeekFormat,
FirstDayOfWeek = FirstDayOfWeek,
SourceType = GetSourcePropertyType(dataItem),
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalAlignment = HorizontalAlignment.Stretch,
};
timePicker.SetBinding(TableViewDatePicker.SelectedDateProperty, Binding);
return timePicker;
}
/// <inheritdoc/>
protected internal override object? PrepareCellForEdit(TableViewCell cell, RoutedEventArgs routedEvent)
{
if (cell.Content is TableViewDatePicker datePicker)
{
return datePicker.SelectedDate;
}
return base.PrepareCellForEdit(cell, routedEvent);
}
/// <inheritdoc/>
protected internal override void EndCellEditing(TableViewCell cell, object? dataItem, TableViewEditAction editAction, object? uneditedValue)
{
if (cell.Content is TableViewDatePicker datePicker)
{
if (editAction == TableViewEditAction.Commit)
{
var bindingExpression = datePicker.GetBindingExpression(TableViewDatePicker.SelectedDateProperty);
bindingExpression?.UpdateSource();
}
}
}
/// <summary>
/// Gets the type of the source property.
/// </summary>
/// <param name="dataItem">The data item associated with the cell.</param>
/// <returns>The type of the source property.</returns>
private Type? GetSourcePropertyType(object? dataItem)
{
if (Binding is not null && dataItem is not null)
{
var type = dataItem.GetType();
if (!string.IsNullOrEmpty(PropertyPath))
{
var propertyInfo = type.GetProperty(PropertyPath);
if (propertyInfo is not null)
{
type = propertyInfo.PropertyType;
}
}
if (type.IsDateOnly() || type.IsDateTime() || type.IsDateTimeOffset())
{
return type;
}
}
return typeof(DateTimeOffset);
}
/// <summary>
/// Gets or sets the date format for the column.
/// </summary>
public string? DateFormat
{
get => (string?)GetValue(DateFormatProperty);
set => SetValue(DateFormatProperty, value);
}
/// <summary>
/// Gets or sets the minimum date for the date picker.
/// </summary>
public DateTimeOffset MinDate
{
get => (DateTimeOffset)GetValue(MinDateProperty);
set => SetValue(MinDateProperty, value);
}
/// <summary>
/// Gets or sets the maximum date for the date picker.
/// </summary>
public DateTimeOffset MaxDate
{
get => (DateTimeOffset)GetValue(MaxDateProperty);
set => SetValue(MaxDateProperty, value);
}
/// <summary>
/// Gets or sets a value indicating whether today's date is highlighted in date picker.
/// </summary>
public bool IsTodayHighlighted
{
get => (bool)GetValue(IsTodayHighlightedProperty);
set => SetValue(IsTodayHighlightedProperty, value);
}
/// <summary>
/// Gets or sets a value indicating whether out-of-scope dates are enabled in date picker.
/// </summary>
public bool IsOutOfScopeEnabled
{
get => (bool)GetValue(IsOutOfScopeEnabledProperty);
set => SetValue(IsOutOfScopeEnabledProperty, value);
}
/// <summary>
/// Gets or sets a value indicating whether the group label is visible in date picker.
/// </summary>
public bool IsGroupLabelVisible
{
get => (bool)GetValue(IsGroupLabelVisibleProperty);
set => SetValue(IsGroupLabelVisibleProperty, value);
}
/// <summary>
/// Gets or sets the placeholder text for the date picker in date picker.
/// </summary>
public string? PlaceHolderText
{
get => (string?)GetValue(PlaceHolderTextProperty);
set => SetValue(PlaceHolderTextProperty, value);
}
/// <summary>
/// Gets or sets the format for the day of the week in date picker.
/// </summary>
public string DayOfWeekFormat
{
get => (string)GetValue(DayOfWeekFormatProperty);
set => SetValue(DayOfWeekFormatProperty, value);
}
/// <summary>
/// Gets or sets the first day of the week in date picker.
/// </summary>
public DayOfWeek FirstDayOfWeek
{
get => (DayOfWeek)GetValue(FirstDayOfWeekProperty);
set => SetValue(FirstDayOfWeekProperty, value);
}
/// <summary>
/// Identifies the FirstDayOfWeek dependency property.
/// </summary>
public static readonly DependencyProperty FirstDayOfWeekProperty = DependencyProperty.Register(nameof(FirstDayOfWeek), typeof(DayOfWeek), typeof(TableViewDateColumn), new PropertyMetadata(DayOfWeek.Sunday));
/// <summary>
/// Identifies the DayOfWeekFormat dependency property.
/// </summary>
public static readonly DependencyProperty DayOfWeekFormatProperty = DependencyProperty.Register(nameof(DayOfWeekFormat), typeof(string), typeof(TableViewDateColumn), new PropertyMetadata(string.Empty));
/// <summary>
/// Identifies the PlaceHolderText dependency property.
/// </summary>
public static readonly DependencyProperty PlaceHolderTextProperty = DependencyProperty.Register(nameof(PlaceHolderText), typeof(string), typeof(TableViewDateColumn), new PropertyMetadata(null));
/// <summary>
/// Identifies the IsGroupLabelVisible dependency property.
/// </summary>
public static readonly DependencyProperty IsGroupLabelVisibleProperty = DependencyProperty.Register(nameof(IsGroupLabelVisible), typeof(bool), typeof(TableViewDateColumn), new PropertyMetadata(true));
/// <summary>
/// Identifies the IsOutOfScopeEnabled dependency property.
/// </summary>
public static readonly DependencyProperty IsOutOfScopeEnabledProperty = DependencyProperty.Register(nameof(IsOutOfScopeEnabled), typeof(bool), typeof(TableViewDateColumn), new PropertyMetadata(true));
/// <summary>
/// Identifies the IsTodayHighlighted dependency property.
/// </summary>
public static readonly DependencyProperty IsTodayHighlightedProperty = DependencyProperty.Register(nameof(IsTodayHighlighted), typeof(bool), typeof(TableViewDateColumn), new PropertyMetadata(true));
/// <summary>
/// Identifies the MinDate dependency property.
/// </summary>
public static readonly DependencyProperty MinDateProperty = DependencyProperty.Register(nameof(MinDate), typeof(DateTimeOffset), typeof(TableViewDateColumn), new PropertyMetadata(DateTimeOffset.MinValue));
/// <summary>
/// Identifies the MaxDate dependency property.
/// </summary>
public static readonly DependencyProperty MaxDateProperty = DependencyProperty.Register(nameof(MaxDate), typeof(DateTimeOffset), typeof(TableViewDateColumn), new PropertyMetadata(DateTimeOffset.MaxValue));
/// <summary>
/// Identifies the DateFormat dependency property.
/// </summary>
public static readonly DependencyProperty DateFormatProperty = DependencyProperty.Register(nameof(DateFormat), typeof(string), typeof(TableViewDateColumn), new PropertyMetadata("shortdate"));
}