fix:修复DateTimePicker VM Binding属性设置为null时 UI不会同步更新的问题

This commit is contained in:
尹志芳 authored and NaBian committed 2025-11-12 21:36:38 +08:00
1 parent 81d0c3d248
commit d7b4361c49
1 file changed
+15 -3
@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Windows; using System.Windows;
@@ -183,6 +183,11 @@ public class DateTimePicker : Control
var time = dp.SelectedDateTime.Value; var time = dp.SelectedDateTime.Value;
dp.SetTextInternal(dp.DateTimeToString(time)); dp.SetTextInternal(dp.DateTimeToString(time));
} }
else
{
// 修复:当 SelectedDateTime 为 null 时,清空文本框
dp.SetTextInternal(string.Empty);
}
dp.RaiseEvent(new FunctionEventArgs<DateTime?>(SelectedDateTimeChangedEvent, dp) dp.RaiseEvent(new FunctionEventArgs<DateTime?>(SelectedDateTimeChangedEvent, dp)
{ {
@@ -620,7 +625,8 @@ public class DateTimePicker : Control
} }
var d = SetTextBoxValue(s); var d = SetTextBoxValue(s);
if (!SelectedDateTime.Equals(d)) // 修复:使用静态的 Equals 方法进行安全的 null 比较
if (!Equals(SelectedDateTime, d))
{ {
SetCurrentValue(SelectedDateTimeProperty, d); SetCurrentValue(SelectedDateTimeProperty, d);
SetCurrentValue(DisplayDateTimeProperty, d); SetCurrentValue(DisplayDateTimeProperty, d);
@@ -632,12 +638,18 @@ public class DateTimePicker : Control
{ {
SetCurrentValue(SelectedDateTimeProperty, null); SetCurrentValue(SelectedDateTimeProperty, null);
} }
else
{
// 修复:确保文本框同步清空
SetTextInternal(string.Empty);
}
} }
} }
else else
{ {
var d = SetTextBoxValue(_defaultText); var d = SetTextBoxValue(_defaultText);
if (!SelectedDateTime.Equals(d)) // 修复:使用静态的 Equals 方法进行安全的 null 比较
if (!Equals(SelectedDateTime, d))
{ {
SetCurrentValue(SelectedDateTimeProperty, d); SetCurrentValue(SelectedDateTimeProperty, d);
} }