Skip to content

Commit d7b4361

Browse files
yinzhifangNaBian
authored andcommitted
fix:修复DateTimePicker VM Binding属性设置为null时 UI不会同步更新的问题
1 parent 81d0c3d commit d7b4361

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
44
using System.Windows;
@@ -183,6 +183,11 @@ private static void OnSelectedDateTimeChanged(DependencyObject d, DependencyProp
183183
var time = dp.SelectedDateTime.Value;
184184
dp.SetTextInternal(dp.DateTimeToString(time));
185185
}
186+
else
187+
{
188+
// 修复:当 SelectedDateTime 为 null 时,清空文本框
189+
dp.SetTextInternal(string.Empty);
190+
}
186191

187192
dp.RaiseEvent(new FunctionEventArgs<DateTime?>(SelectedDateTimeChangedEvent, dp)
188193
{
@@ -620,7 +625,8 @@ private void SetSelectedDateTime()
620625
}
621626

622627
var d = SetTextBoxValue(s);
623-
if (!SelectedDateTime.Equals(d))
628+
// 修复:使用静态的 Equals 方法进行安全的 null 比较
629+
if (!Equals(SelectedDateTime, d))
624630
{
625631
SetCurrentValue(SelectedDateTimeProperty, d);
626632
SetCurrentValue(DisplayDateTimeProperty, d);
@@ -632,12 +638,18 @@ private void SetSelectedDateTime()
632638
{
633639
SetCurrentValue(SelectedDateTimeProperty, null);
634640
}
641+
else
642+
{
643+
// 修复:确保文本框同步清空
644+
SetTextInternal(string.Empty);
645+
}
635646
}
636647
}
637648
else
638649
{
639650
var d = SetTextBoxValue(_defaultText);
640-
if (!SelectedDateTime.Equals(d))
651+
// 修复:使用静态的 Equals 方法进行安全的 null 比较
652+
if (!Equals(SelectedDateTime, d))
641653
{
642654
SetCurrentValue(SelectedDateTimeProperty, d);
643655
}

0 commit comments

Comments
 (0)