-
-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathTableViewTimeColumn.cs
More file actions
173 lines (150 loc) · 6 KB
/
Copy pathTableViewTimeColumn.cs
File metadata and controls
173 lines (150 loc) · 6 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
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using System;
using Windows.System.UserProfile;
using WinUI.TableView.Controls;
using WinUI.TableView.Extensions;
using WinUI.TableView.Helpers;
namespace WinUI.TableView;
/// <summary>
/// Represents a column in a TableView that displays time.
/// </summary>
[StyleTypedProperty(Property = nameof(ElementStyle), StyleTargetType = typeof(TextBlock))]
[StyleTypedProperty(Property = nameof(EditingElementStyle), StyleTargetType = typeof(TableViewTimePicker))]
#if WINDOWS
[WinRT.GeneratedBindableCustomProperty]
#endif
public partial class TableViewTimeColumn : TableViewBoundColumn
{
/// <summary>
/// Initializes a new instance of the TableViewTimeColumn class.
/// </summary>
public TableViewTimeColumn()
{
var clocks = GlobalizationPreferences.Clocks;
ClockIdentifier = clocks.Count > 0 ? clocks[0] : "24HourClock";
}
/// <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(ClockIdentifier)),
Source = this
});
return textBlock;
}
/// <summary>
/// Generates a TableViewTimePicker 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 TableViewTimePicker element.</returns>
public override FrameworkElement GenerateEditingElement(TableViewCell cell, object? dataItem)
{
var timePicker = new TableViewTimePicker
{
ClockIdentifier = ClockIdentifier,
MinuteIncrement = MinuteIncrement,
PlaceholderText = PlaceholderText ?? TableViewLocalizedStrings.TimePickerPlaceholder,
SourceType = GetSourcePropertyType(dataItem),
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalAlignment = HorizontalAlignment.Stretch,
};
timePicker.SetBinding(TableViewTimePicker.SelectedTimeProperty, Binding);
return timePicker;
}
/// <inheritdoc/>
protected internal override object? PrepareCellForEdit(TableViewCell cell, RoutedEventArgs routedEvent)
{
if (cell.Content is TableViewTimePicker timePicker)
{
return timePicker.SelectedTime;
}
return base.PrepareCellForEdit(cell, routedEvent);
}
/// <inheritdoc/>
protected internal override void EndCellEditing(TableViewCell cell, object? dataItem, TableViewEditAction editAction, object? uneditedValue)
{
if (cell.Content is TableViewTimePicker timePicker)
{
if (editAction == TableViewEditAction.Commit)
{
var bindingExpression = timePicker.GetBindingExpression(TimePicker.SelectedTimeProperty);
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.IsTimeSpan() || type.IsTimeOnly() || type.IsDateTime() || type.IsDateTimeOffset())
{
return type;
}
}
return typeof(TimeSpan);
}
/// <summary>
/// Gets or sets the clock identifier for the time picker.
/// </summary>
public string ClockIdentifier
{
get => (string)GetValue(ClockIdentifierProperty);
set => SetValue(ClockIdentifierProperty, value);
}
/// <summary>
/// Gets or sets the minute increment for the time picker.
/// </summary>
public int MinuteIncrement
{
get => (int)GetValue(MinuteIncrementProperty);
set => SetValue(MinuteIncrementProperty, value);
}
/// <summary>
/// Gets or sets the placeholder text for the time picker.
/// </summary>
public string? PlaceholderText
{
get => (string?)GetValue(PlaceholderTextProperty);
set => SetValue(PlaceholderTextProperty, value);
}
/// <summary>
/// Identifies the MinuteIncrement dependency property.
/// </summary>
public static readonly DependencyProperty MinuteIncrementProperty = DependencyProperty.Register(nameof(MinuteIncrement), typeof(int), typeof(TableViewTimeColumn), new PropertyMetadata(1));
/// <summary>
/// Identifies the ClockIdentifier dependency property.
/// </summary>
public static readonly DependencyProperty ClockIdentifierProperty = DependencyProperty.Register(nameof(ClockIdentifier), typeof(string), typeof(TableViewTimeColumn), new PropertyMetadata(default));
/// <summary>
/// Identifies the PlaceholderText dependency property.
/// </summary>
public static readonly DependencyProperty PlaceholderTextProperty = DependencyProperty.Register(nameof(PlaceholderText), typeof(string), typeof(TableViewTimeColumn), new PropertyMetadata(null));
}