1- using System ;
1+ using System ;
22using System . Collections . Generic ;
33using System . ComponentModel ;
44using 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