From a7b2d297a7001119bd4eac734ab79466edca52fd Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Sat, 31 Jul 2021 14:18:53 +0800 Subject: [PATCH] fixed #913 --- .../Controls/Input/DateTimePicker.cs | 19 ++++++++++++----- .../Controls/Input/TimePicker.cs | 21 ++++++++++++++----- .../Controls/Time/CalendarWithClock.cs | 1 + 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs b/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs index 279771b2..db1543c3 100644 --- a/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs +++ b/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs @@ -367,9 +367,11 @@ namespace HandyControl.Controls _dropDownButton.Click += DropDownButton_Click; _dropDownButton.MouseLeave += DropDownButton_MouseLeave; + var selectedDateTime = SelectedDateTime; + if (_textBox != null) { - if (SelectedDateTime == null) + if (selectedDateTime == null) { _textBox.Text = DateTime.Now.ToString(DateTimeFormat); } @@ -385,7 +387,7 @@ namespace HandyControl.Controls _textBox.TextChanged += TextBox_TextChanged; _textBox.LostFocus += TextBox_LostFocus; - if (SelectedDateTime == null) + if (selectedDateTime == null) { if (!string.IsNullOrEmpty(_defaultText)) { @@ -395,12 +397,19 @@ namespace HandyControl.Controls } else { - _textBox.Text = DateTimeToString(SelectedDateTime.Value); + _textBox.Text = DateTimeToString(selectedDateTime.Value); } } - _originalSelectedDateTime ??= DateTime.Now; - SetCurrentValue(DisplayDateTimeProperty, _originalSelectedDateTime); + if (selectedDateTime is null) + { + _originalSelectedDateTime ??= DateTime.Now; + SetCurrentValue(DisplayDateTimeProperty, _originalSelectedDateTime); + } + else + { + SetCurrentValue(DisplayDateTimeProperty, selectedDateTime); + } } public virtual bool VerifyData() diff --git a/src/Shared/HandyControl_Shared/Controls/Input/TimePicker.cs b/src/Shared/HandyControl_Shared/Controls/Input/TimePicker.cs index a91d98d6..6bc7a5c9 100644 --- a/src/Shared/HandyControl_Shared/Controls/Input/TimePicker.cs +++ b/src/Shared/HandyControl_Shared/Controls/Input/TimePicker.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; @@ -194,6 +195,7 @@ namespace HandyControl.Controls if (dp.SelectedTime.HasValue) { var time = dp.SelectedTime.Value; + Trace.WriteLine(time); dp.SetTextInternal(dp.DateTimeToString(time)); } @@ -462,9 +464,11 @@ namespace HandyControl.Controls _dropDownButton.Click += DropDownButton_Click; _dropDownButton.MouseLeave += DropDownButton_MouseLeave; + var selectedTime = SelectedTime; + if (_textBox != null) { - if (SelectedTime == null) + if (selectedTime == null) { _textBox.Text = DateTime.Now.ToString(TimeFormat); } @@ -480,7 +484,7 @@ namespace HandyControl.Controls _textBox.TextChanged += TextBox_TextChanged; _textBox.LostFocus += TextBox_LostFocus; - if (SelectedTime == null) + if (selectedTime == null) { if (!string.IsNullOrEmpty(_defaultText)) { @@ -490,12 +494,19 @@ namespace HandyControl.Controls } else { - _textBox.Text = DateTimeToString((DateTime) SelectedTime); + _textBox.Text = DateTimeToString(selectedTime.Value); } } - _originalSelectedTime ??= DateTime.Now; - SetCurrentValue(DisplayTimeProperty, _originalSelectedTime); + if (selectedTime is null) + { + _originalSelectedTime ??= DateTime.Now; + SetCurrentValue(DisplayTimeProperty, _originalSelectedTime); + } + else + { + SetCurrentValue(DisplayTimeProperty, selectedTime); + } } public override string ToString() => SelectedTime?.ToString(TimeFormat) ?? string.Empty; diff --git a/src/Shared/HandyControl_Shared/Controls/Time/CalendarWithClock.cs b/src/Shared/HandyControl_Shared/Controls/Time/CalendarWithClock.cs index fa08563e..57b2237b 100644 --- a/src/Shared/HandyControl_Shared/Controls/Time/CalendarWithClock.cs +++ b/src/Shared/HandyControl_Shared/Controls/Time/CalendarWithClock.cs @@ -119,6 +119,7 @@ namespace HandyControl.Controls var v = (DateTime) e.NewValue; ctl._clock.SelectedTime = v; ctl._calendar.SelectedDate = v; + ctl._calendar.DisplayDate = v; ctl.OnDisplayDateTimeChanged(new FunctionEventArgs(v)); }