From d7b4361c49980af6d967f7fbd94eb9b29ff4a6e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B9=E5=BF=97=E8=8A=B3?= Date: Thu, 23 Oct 2025 19:25:43 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E5=A4=8DDateTimePicker=20?= =?UTF-8?q?VM=20Binding=E5=B1=9E=E6=80=A7=E8=AE=BE=E7=BD=AE=E4=B8=BAnull?= =?UTF-8?q?=E6=97=B6=20UI=E4=B8=8D=E4=BC=9A=E5=90=8C=E6=AD=A5=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controls/Input/DateTimePicker.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs b/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs index 142d593e..3f8c1295 100644 --- a/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs +++ b/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows; @@ -183,6 +183,11 @@ public class DateTimePicker : Control var time = dp.SelectedDateTime.Value; dp.SetTextInternal(dp.DateTimeToString(time)); } + else + { + // 修复:当 SelectedDateTime 为 null 时,清空文本框 + dp.SetTextInternal(string.Empty); + } dp.RaiseEvent(new FunctionEventArgs(SelectedDateTimeChangedEvent, dp) { @@ -620,7 +625,8 @@ public class DateTimePicker : Control } var d = SetTextBoxValue(s); - if (!SelectedDateTime.Equals(d)) + // 修复:使用静态的 Equals 方法进行安全的 null 比较 + if (!Equals(SelectedDateTime, d)) { SetCurrentValue(SelectedDateTimeProperty, d); SetCurrentValue(DisplayDateTimeProperty, d); @@ -632,12 +638,18 @@ public class DateTimePicker : Control { SetCurrentValue(SelectedDateTimeProperty, null); } + else + { + // 修复:确保文本框同步清空 + SetTextInternal(string.Empty); + } } } else { var d = SetTextBoxValue(_defaultText); - if (!SelectedDateTime.Equals(d)) + // 修复:使用静态的 Equals 方法进行安全的 null 比较 + if (!Equals(SelectedDateTime, d)) { SetCurrentValue(SelectedDateTimeProperty, d); }