@@ -77,7 +77,10 @@ private static void TextPropertyChangedCallback(DependencyObject dependencyObjec
77
77
{
78
78
var timePicker = ( TimePicker ) dependencyObject ;
79
79
if ( ! timePicker . _isManuallyMutatingText )
80
+ {
80
81
timePicker . SetSelectedTime ( ) ;
82
+ }
83
+
81
84
if ( timePicker . _textBox != null )
82
85
{
83
86
timePicker . UpdateTextBoxText ( dependencyPropertyChangedEventArgs . NewValue as string ?? "" ) ;
@@ -173,13 +176,7 @@ public bool Is24Hours
173
176
private static object OnCoerceIsDropDownOpen ( DependencyObject d , object baseValue )
174
177
{
175
178
var timePicker = ( TimePicker ) d ;
176
-
177
- if ( ! timePicker . IsEnabled )
178
- {
179
- return false ;
180
- }
181
-
182
- return baseValue ;
179
+ return timePicker . IsEnabled ? baseValue : false ;
183
180
}
184
181
185
182
/// <summary>
@@ -326,7 +323,9 @@ private void TextBoxOnLostFocus(object sender, RoutedEventArgs routedEventArgs)
326
323
UpdateTextBoxTextIfNeeded ( text ! ) ;
327
324
}
328
325
else // Invalid time, jump back to previous good time
326
+ {
329
327
SetInvalidTime ( ) ;
328
+ }
330
329
}
331
330
332
331
private void SetInvalidTime ( )
@@ -352,9 +351,7 @@ private void SetInvalidTime()
352
351
}
353
352
354
353
private void TextBoxOnKeyDown ( object sender , KeyEventArgs keyEventArgs )
355
- {
356
- keyEventArgs . Handled = ProcessKey ( keyEventArgs ) || keyEventArgs . Handled ;
357
- }
354
+ => keyEventArgs . Handled = ProcessKey ( keyEventArgs ) || keyEventArgs . Handled ;
358
355
359
356
private bool ProcessKey ( KeyEventArgs keyEventArgs )
360
357
{
@@ -394,19 +391,23 @@ private void TextBoxOnTextChanged(object sender, TextChangedEventArgs textChange
394
391
if ( _textBox is { } textBox &&
395
392
( _popup ? . IsOpen == true || IsInvalidTextAllowed ) )
396
393
{
394
+ _isManuallyMutatingText = true ;
397
395
SetCurrentValue ( TextProperty , textBox . Text ) ;
396
+ _isManuallyMutatingText = false ;
398
397
}
399
398
400
399
if ( _popup ? . IsOpen == false )
400
+ {
401
401
SetSelectedTime ( true ) ;
402
+ }
402
403
}
403
404
404
405
private void UpdateTextBoxText ( string ? text )
405
406
{
406
407
// Save and restore the cursor position
407
408
if ( _textBox is { } textBox )
408
409
{
409
- var caretIndex = textBox . CaretIndex ;
410
+ int caretIndex = textBox . CaretIndex ;
410
411
textBox . Text = text ;
411
412
textBox . CaretIndex = caretIndex ;
412
413
}
@@ -418,26 +419,31 @@ private void UpdateTextBoxTextIfNeeded(string lastText)
418
419
{
419
420
string ? formattedText = DateTimeToString ( SelectedTime ) ;
420
421
if ( formattedText != lastText )
422
+ {
421
423
UpdateTextBoxText ( formattedText ) ;
424
+ }
422
425
}
423
426
}
424
427
425
428
private void SetSelectedTime ( in DateTime time )
426
- {
427
- SetCurrentValue ( SelectedTimeProperty , ( SelectedTime ? . Date ?? DateTime . Today ) . Add ( time . TimeOfDay ) ) ;
428
- }
429
+ => SetCurrentValue ( SelectedTimeProperty , ( SelectedTime ? . Date ?? DateTime . Today ) . Add ( time . TimeOfDay ) ) ;
429
430
430
431
private void SetSelectedTime ( bool beCautious = false )
431
432
{
432
- var currentText = _textBox ? . Text ;
433
+ string ? currentText = _textBox ? . Text ;
433
434
if ( ! string . IsNullOrEmpty ( currentText ) )
434
435
{
435
436
ParseTime ( currentText ! , t =>
436
437
{
437
438
if ( ! beCautious || DateTimeToString ( t ) == currentText )
439
+ {
438
440
SetSelectedTime ( t ) ;
441
+ }
442
+
439
443
if ( ! beCautious )
444
+ {
440
445
UpdateTextBoxTextIfNeeded ( currentText ! ) ;
446
+ }
441
447
} ) ;
442
448
}
443
449
else
@@ -449,7 +455,9 @@ private void SetSelectedTime(bool beCautious = false)
449
455
private void ParseTime ( string s , Action < DateTime > successContinuation )
450
456
{
451
457
if ( IsTimeValid ( s , out DateTime time ) )
458
+ {
452
459
successContinuation ( time ) ;
460
+ }
453
461
}
454
462
455
463
private bool IsTimeValid ( string s , out DateTime time )
@@ -500,7 +508,7 @@ private string DateTimeToString(DateTime datetime, DatePickerFormat format)
500
508
501
509
private void PopupOnPreviewMouseLeftButtonDown ( object sender , MouseButtonEventArgs mouseButtonEventArgs )
502
510
{
503
- if ( ! ( sender is Popup popup ) || popup . StaysOpen ) return ;
511
+ if ( sender is not Popup popup || popup . StaysOpen ) return ;
504
512
505
513
if ( _dropDownButton ? . InputHitTest ( mouseButtonEventArgs . GetPosition ( _dropDownButton ) ) != null )
506
514
{
@@ -573,11 +581,15 @@ private void DropDownButtonOnClick(object sender, RoutedEventArgs routedEventArg
573
581
private void TogglePopup ( )
574
582
{
575
583
if ( IsDropDownOpen )
584
+ {
576
585
SetCurrentValue ( IsDropDownOpenProperty , false ) ;
586
+ }
577
587
else
578
588
{
579
589
if ( _disablePopupReopen )
590
+ {
580
591
_disablePopupReopen = false ;
592
+ }
581
593
else
582
594
{
583
595
SetSelectedTime ( ) ;
0 commit comments