Skip to content

Commit f224f06

Browse files
authored
Merge pull request #122 from w-ahmad/uno-platform
added uno support
2 parents 90931a3 + 9482b8e commit f224f06

22 files changed

+369
-188
lines changed

src/Columns/TableViewColumn.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
namespace WinUI.TableView;
55

6+
/// <summary>
67
/// Represents a column in a TableView.
78
/// </summary>
89
[StyleTypedProperty(Property = nameof(HeaderStyle), StyleTargetType = typeof(TableViewColumnHeader))]
910
[StyleTypedProperty(Property = nameof(CellStyle), StyleTargetType = typeof(TableViewCell))]
10-
public abstract class TableViewColumn : DependencyObject
11+
public abstract partial class TableViewColumn : DependencyObject
1112
{
1213
private TableViewColumnHeader? _headerControl;
1314
private double _desiredWidth;
@@ -393,7 +394,7 @@ private static void OnCellStyleChanged(DependencyObject d, DependencyPropertyCha
393394
/// <summary>
394395
/// Identifies the CellStyle dependency property.
395396
/// </summary>
396-
public static readonly DependencyProperty CellStyleProperty = DependencyProperty.Register(nameof(CellStyle), typeof(Style), typeof(TableView), new PropertyMetadata(null, OnCellStyleChanged));
397+
public static readonly DependencyProperty CellStyleProperty = DependencyProperty.Register(nameof(CellStyle), typeof(Style), typeof(TableViewColumn), new PropertyMetadata(null, OnCellStyleChanged));
397398

398399
/// <summary>
399400
/// Identifies the Header dependency property.

src/Columns/TableViewDateColumn.cs

+1-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.UI.Xaml.Controls;
33
using Microsoft.UI.Xaml.Data;
44
using System;
5-
using Windows.Globalization.DateTimeFormatting;
65
using WinUI.TableView.Controls;
76
using WinUI.TableView.Extensions;
87
using WinUI.TableView.Helpers;
@@ -17,14 +16,6 @@ namespace WinUI.TableView;
1716
[StyleTypedProperty(Property = nameof(EditingElementStyle), StyleTargetType = typeof(TableViewDatePicker))]
1817
public partial class TableViewDateColumn : TableViewBoundColumn
1918
{
20-
/// <summary>
21-
/// Initializes a new instance of the TableViewDateColumn class.
22-
/// </summary>
23-
public TableViewDateColumn()
24-
{
25-
DateFormat = DateTimeFormatter.ShortDate.Patterns[0];
26-
}
27-
2819
/// <summary>
2920
/// Generates a TextBlock element for the cell.
3021
/// </summary>
@@ -230,5 +221,5 @@ public DayOfWeek FirstDayOfWeek
230221
/// <summary>
231222
/// Identifies the DateFormat dependency property.
232223
/// </summary>
233-
public static readonly DependencyProperty DateFormatProperty = DependencyProperty.Register(nameof(DateFormat), typeof(string), typeof(TableViewDateColumn), new PropertyMetadata(default));
224+
public static readonly DependencyProperty DateFormatProperty = DependencyProperty.Register(nameof(DateFormat), typeof(string), typeof(TableViewDateColumn), new PropertyMetadata("shortdate"));
234225
}

src/Columns/TableViewTimeColumn.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public partial class TableViewTimeColumn : TableViewBoundColumn
2121
/// </summary>
2222
public TableViewTimeColumn()
2323
{
24-
ClockIdentifier = DateTimeFormatter.ShortTime.Clock;
24+
ClockIdentifier = DateTimeFormatter.LongTime.Clock;
2525
}
2626

2727
/// <summary>

src/Controls/TableViewTimePicker.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private void UpdateTimeText()
110110
{
111111
if (_timeText is null) return;
112112

113-
var formatter = ClockIdentifier is "24HourClock" ? DateTimeFormatHelper._24HourClockFormatter : DateTimeFormatHelper._12HourClockFormatter;
113+
var formatter = DateTimeFormatHelper.GetDateTimeFormatter("shortime", ClockIdentifier);
114114

115115
_timeText.Text = SelectedTime switch
116116
{

src/Extensions/DateTimeExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace WinUI.TableView.Extensions;
44

55
/// <summary>
6-
/// Provides extension methods for Date&Time types.
6+
/// Provides extension methods for Date and Time types.
77
/// </summary>
88
internal static class DateTimeExtensions
99
{

src/Helpers/DateTimeFormatHelper.cs

+30-27
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,23 @@
11
using Microsoft.UI.Xaml;
22
using Microsoft.UI.Xaml.Controls;
33
using System;
4+
using System.Collections.Generic;
45
using System.Diagnostics;
6+
using System.Globalization;
57
using Windows.Globalization.DateTimeFormatting;
68
using Windows.System.UserProfile;
79
using WinUI.TableView.Extensions;
810

911
namespace WinUI.TableView.Helpers;
1012

1113
/// <summary>
12-
/// Provides helper methods for formatting Date&Time values.
14+
/// Provides helper methods for formatting Date and Time values.
1315
/// </summary>
1416
internal static class DateTimeFormatHelper
1517
{
1618
private const string _12HourClock = "12HourClock";
1719
private const string _24HourClock = "24HourClock";
18-
19-
/// <summary>
20-
/// Gets the DateTimeFormatter for 12-hour clock format.
21-
/// </summary>
22-
internal static DateTimeFormatter _12HourClockFormatter { get; } = GetClockFormatter(_12HourClock);
23-
24-
/// <summary>
25-
/// Gets the DateTimeFormatter for 24-hour clock format.
26-
/// </summary>
27-
internal static DateTimeFormatter _24HourClockFormatter { get; } = GetClockFormatter(_24HourClock);
28-
29-
/// <summary>
30-
/// Gets a DateTimeFormatter for the specified clock format.
31-
/// </summary>
32-
/// <param name="clock">The clock format ("12HourClock" or "24HourClock").</param>
33-
/// <returns>A DateTimeFormatter for the specified clock format.</returns>
34-
private static DateTimeFormatter GetClockFormatter(string clock)
35-
{
36-
var languages = GlobalizationPreferences.Languages;
37-
var geographicRegion = GlobalizationPreferences.HomeGeographicRegion;
38-
var calendar = GlobalizationPreferences.Calendars[0];
39-
40-
return new DateTimeFormatter("shorttime", languages, geographicRegion, calendar, clock);
41-
}
20+
private static readonly Dictionary<(string Format, string? Clock), DateTimeFormatter> _formatters = [];
4221

4322
/// <summary>
4423
/// Sets the formatted text for a TextBlock based on its value and format.
@@ -53,7 +32,7 @@ private static void SetFormattedText(TextBlock textBlock)
5332
{
5433
if (value is not null && format is _12HourClock or _24HourClock)
5534
{
56-
var formatter = format is _24HourClock ? _24HourClockFormatter : _12HourClockFormatter;
35+
var formatter = GetDateTimeFormatter("shorttime", format);
5736
var dateTimeOffset = value switch
5837
{
5938
TimeSpan timeSpan => timeSpan.ToDateTimeOffset(),
@@ -67,7 +46,8 @@ private static void SetFormattedText(TextBlock textBlock)
6746
}
6847
else if (value is not null)
6948
{
70-
var formatter = new DateTimeFormatter(format);
49+
var formatter = GetDateTimeFormatter(format);
50+
7151
var dateTimeOffset = value switch
7252
{
7353
DateOnly dateOnly => dateOnly.ToDateTimeOffset(),
@@ -90,6 +70,29 @@ private static void SetFormattedText(TextBlock textBlock)
9070
}
9171
}
9272

73+
/// <summary>
74+
/// Gets a DateTimeFormatter for the specified format and clock.
75+
/// </summary>
76+
internal static DateTimeFormatter GetDateTimeFormatter(string strFormat, string? strClock = null)
77+
{
78+
if (_formatters.TryGetValue((strFormat, strClock), out var cacheFormatter))
79+
{
80+
return cacheFormatter;
81+
}
82+
83+
var formatter = new DateTimeFormatter(strFormat);
84+
var result = new DateTimeFormatter(
85+
strFormat,
86+
formatter.Languages,
87+
formatter.GeographicRegion,
88+
formatter.Calendar,
89+
strClock ?? formatter.Clock);
90+
91+
_formatters[(strFormat, strClock)] = result;
92+
93+
return result;
94+
}
95+
9396
/// <summary>
9497
/// Handles changes to the Value attached property.
9598
/// </summary>

src/TableView.Properties.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public partial class TableView
125125
/// </summary>
126126
public static readonly DependencyProperty VerticalGridLinesStrokeProperty = DependencyProperty.Register(nameof(VerticalGridLinesStroke), typeof(Brush), typeof(TableView), new PropertyMetadata(default, OnGridLinesPropertyChanged));
127127

128+
#if WINDOWS
128129
/// <summary>
129130
/// Identifies the AlternateRowForeground dependency property.
130131
/// </summary>
@@ -144,7 +145,7 @@ public partial class TableView
144145
/// Identifies the CellContextFlyout dependency property.
145146
/// </summary>
146147
public static readonly DependencyProperty CellContextFlyoutProperty = DependencyProperty.Register(nameof(CellContextFlyout), typeof(FlyoutBase), typeof(TableView), new PropertyMetadata(null));
147-
148+
#endif
148149
/// <summary>
149150
/// Identifies the ColumnHeaderStyle dependency property.
150151
/// </summary>
@@ -430,6 +431,7 @@ public Brush HorizontalGridLinesStroke
430431
set => SetValue(HorizontalGridLinesStrokeProperty, value);
431432
}
432433

434+
#if WINDOWS
433435
/// <summary>
434436
/// Gets or sets the background brush for alternate rows.
435437
/// </summary>
@@ -465,6 +467,7 @@ public FlyoutBase? CellContextFlyout
465467
get => (FlyoutBase?)GetValue(CellContextFlyoutProperty);
466468
set => SetValue(CellContextFlyoutProperty, value);
467469
}
470+
#endif
468471

469472
/// <summary>
470473
/// Gets or sets the style applied to all column headers.
@@ -491,7 +494,7 @@ private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyC
491494
{
492495
if (d is TableView tableView)
493496
{
494-
tableView.OnItemsSourceChanged(e);
497+
tableView.ItemsSourceChanged(e);
495498
tableView.SelectedCellRanges.Clear();
496499
tableView.OnCellSelectionChanged();
497500
}

0 commit comments

Comments
 (0)