From 7b3b18a0bcf704c28c19f9c1d81f9732cdb57318 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Thu, 18 Jul 2024 21:59:55 +0800 Subject: [PATCH] fix: fix date picker lost focus behavior. --- .../Controls/DateTimePicker/DatePicker.cs | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Ursa/Controls/DateTimePicker/DatePicker.cs b/src/Ursa/Controls/DateTimePicker/DatePicker.cs index eef47d9..b70c152 100644 --- a/src/Ursa/Controls/DateTimePicker/DatePicker.cs +++ b/src/Ursa/Controls/DateTimePicker/DatePicker.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Runtime.CompilerServices; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Metadata; @@ -116,7 +117,13 @@ public class DatePicker: DatePickerBase, IClearControl SetCurrentValue(IsDropdownOpenProperty, true); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void OnTextChanged(object sender, TextChangedEventArgs e) + { + SetSelectedDate(true); + } + + private void SetSelectedDate(bool fromText = false) { if (string.IsNullOrEmpty(_textBox?.Text)) { @@ -145,6 +152,15 @@ public class DatePicker: DatePickerBase, IClearControl } _calendar?.MarkDates(startDate: date, endDate: date); } + else + { + SetCurrentValue(SelectedDateProperty, null); + if (!fromText) + { + _textBox?.SetValue(TextBox.TextProperty, null); + } + _calendar?.ClearSelection(); + } } } @@ -158,7 +174,14 @@ public class DatePicker: DatePickerBase, IClearControl } SetCurrentValue(IsDropdownOpenProperty, true); } - + + protected override void OnLostFocus(RoutedEventArgs e) + { + base.OnLostFocus(e); + SetCurrentValue(IsDropdownOpenProperty, false); + SetSelectedDate(); + } + protected override void OnKeyDown(KeyEventArgs e) { if (e.Key == Key.Escape) @@ -186,6 +209,6 @@ public class DatePicker: DatePickerBase, IClearControl public void Clear() { - + SetCurrentValue(SelectedDateProperty, null); } } \ No newline at end of file