From cb3d219bc5329fcb8ef7153e540cc5178ba594d1 Mon Sep 17 00:00:00 2001 From: QyQj Date: Tue, 8 Nov 2022 10:29:43 +0800 Subject: [PATCH] enhancement: Allow DateTimePicker to choose to use Clock or ListClock --- .../Controls/Input/DateTimePicker.cs | 19 +++++ .../Controls/Time/CalendarWithClock.cs | 70 ++++++++++++++++--- .../Data/Enum/ClockType.cs | 8 +++ .../HandyControl_Shared/Data/ResourceToken.cs | 2 + .../HandyControl_Shared.projitems | 1 + .../Themes/Styles/Clock.xaml | 38 +++++++++- .../HandyControl_Shared/Themes/Theme.xaml | 36 ++++++++++ 7 files changed, 165 insertions(+), 9 deletions(-) create mode 100644 src/Shared/HandyControl_Shared/Data/Enum/ClockType.cs diff --git a/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs b/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs index 51ab33e3d..95f22253f 100644 --- a/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs +++ b/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs @@ -9,6 +9,7 @@ using System.Windows.Media; using System.Windows.Threading; using HandyControl.Data; +using HandyControl.Data.Enum; using HandyControl.Interactivity; namespace HandyControl.Controls; @@ -92,6 +93,24 @@ public DateTimePicker() #region Public Properties + public static readonly DependencyProperty ClockTypeProperty = DependencyProperty.Register( + nameof(ClockType), + typeof(ClockType), + typeof(DateTimePicker), + new PropertyMetadata(ClockType.Clock, ClockStyleChanged)); + + private static void ClockStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var dp = (DateTimePicker) d; + dp._calendarWithClock.ClockType = (ClockType) e.NewValue; + } + + public ClockType ClockType + { + get { return (ClockType) GetValue(ClockTypeProperty); } + set { SetValue(ClockTypeProperty, value); } + } + public static readonly DependencyProperty DateTimeFormatProperty = DependencyProperty.Register( nameof(DateTimeFormat), typeof(string), typeof(DateTimePicker), new PropertyMetadata("yyyy-MM-dd HH:mm:ss")); diff --git a/src/Shared/HandyControl_Shared/Controls/Time/CalendarWithClock.cs b/src/Shared/HandyControl_Shared/Controls/Time/CalendarWithClock.cs index 70e8d2a85..bdc378bda 100644 --- a/src/Shared/HandyControl_Shared/Controls/Time/CalendarWithClock.cs +++ b/src/Shared/HandyControl_Shared/Controls/Time/CalendarWithClock.cs @@ -5,6 +5,8 @@ using System.Windows.Input; using System.Windows.Media; using HandyControl.Data; +using HandyControl.Data.Enum; +using HandyControl.Tools; namespace HandyControl.Controls; @@ -29,7 +31,7 @@ public class CalendarWithClock : Control private ContentPresenter _calendarPresenter; - private Clock _clock; + private ClockBase _clock; private Calendar _calendar; @@ -72,6 +74,25 @@ public CalendarWithClock() #region Public Properties + public static readonly DependencyProperty ClockTypeProperty = DependencyProperty.Register( + nameof(ClockType), + typeof(ClockType), + typeof(CalendarWithClock), + new PropertyMetadata(ClockType.Clock, ClockStyleChanged)); + + private static void ClockStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var dp = (CalendarWithClock) d; + dp.ClockType = (ClockType) e.NewValue; + dp.InitClock(); + } + + public ClockType ClockType + { + get { return (ClockType) GetValue(ClockTypeProperty); } + set { SetValue(ClockTypeProperty, value); } + } + public static readonly DependencyProperty DateTimeFormatProperty = DependencyProperty.Register( nameof(DateTimeFormat), typeof(string), typeof(CalendarWithClock), new PropertyMetadata("yyyy-MM-dd HH:mm:ss")); @@ -214,13 +235,7 @@ private void ButtonConfirm_OnClick(object sender, RoutedEventArgs e) private void InitCalendarAndClock() { - _clock = new Clock - { - BorderThickness = new Thickness(), - Background = Brushes.Transparent - }; - TitleElement.SetBackground(_clock, Brushes.Transparent); - _clock.DisplayTimeChanged += Clock_DisplayTimeChanged; + InitClock(); _calendar = new Calendar { @@ -232,6 +247,45 @@ private void InitCalendarAndClock() _calendar.SelectedDatesChanged += Calendar_SelectedDatesChanged; } + private void InitClock() + { + if (_clock != null) + { + _clock.DisplayTimeChanged -= Clock_DisplayTimeChanged; + } + + switch (ClockType) + { + case ClockType.Clock: + _clock = new Clock + { + BorderThickness = new Thickness(), + Background = Brushes.Transparent + }; + break; + + case ClockType.ListClock: + _clock = new ListClock + { + BorderThickness = new Thickness(), + Background = Brushes.Transparent, + Style = ResourceHelper.GetResourceInternal diff --git a/src/Shared/HandyControl_Shared/Themes/Theme.xaml b/src/Shared/HandyControl_Shared/Themes/Theme.xaml index fe60ec51e..6141f156e 100644 --- a/src/Shared/HandyControl_Shared/Themes/Theme.xaml +++ b/src/Shared/HandyControl_Shared/Themes/Theme.xaml @@ -12862,6 +12862,42 @@