From eb5cce2492afaf2e296c290acbcffbf117e46dcc Mon Sep 17 00:00:00 2001 From: Evan <109839359+Evan260@users.noreply.github.com> Date: Fri, 11 Sep 2026 18:04:06 +0000 Subject: [PATCH] Fix CalendarDatePicker.Focus() not focusing the text box (#22170) * Fix CalendarDatePicker.Focus() not focusing the text box OnGotFocus only forwarded focus to PART_TextBox when the navigation method was Tab, so a programmatic Focus() call left focus on the picker itself. Gate on the focus source instead, and keep select-all on Tab only. * Remove comment from CalendarDatePicker.OnGotFocus --- .../CalendarDatePicker/CalendarDatePicker.cs | 17 +++++--- .../CalendarDatePickerTests.cs | 41 +++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/Avalonia.Controls/CalendarDatePicker/CalendarDatePicker.cs b/src/Avalonia.Controls/CalendarDatePicker/CalendarDatePicker.cs index a96dd013c7..91b7b8029f 100644 --- a/src/Avalonia.Controls/CalendarDatePicker/CalendarDatePicker.cs +++ b/src/Avalonia.Controls/CalendarDatePicker/CalendarDatePicker.cs @@ -396,14 +396,19 @@ namespace Avalonia.Controls protected override void OnGotFocus(FocusChangedEventArgs e) { base.OnGotFocus(e); - if(IsEnabled && _textBox != null && e.NavigationMethod == NavigationMethod.Tab) + + if (IsEnabled && _textBox != null && ReferenceEquals(e.Source, this)) { - _textBox.Focus(); - var text = _textBox.Text; - if(!string.IsNullOrEmpty(text)) + _textBox.Focus(e.NavigationMethod); + + if (e.NavigationMethod == NavigationMethod.Tab) { - _textBox.SelectionStart = 0; - _textBox.SelectionEnd = text.Length; + var text = _textBox.Text; + if (!string.IsNullOrEmpty(text)) + { + _textBox.SelectionStart = 0; + _textBox.SelectionEnd = text.Length; + } } } } diff --git a/tests/Avalonia.Controls.UnitTests/CalendarDatePickerTests.cs b/tests/Avalonia.Controls.UnitTests/CalendarDatePickerTests.cs index 0ec8bd5ef8..00f5b42593 100644 --- a/tests/Avalonia.Controls.UnitTests/CalendarDatePickerTests.cs +++ b/tests/Avalonia.Controls.UnitTests/CalendarDatePickerTests.cs @@ -3,6 +3,8 @@ using System.Linq; using Avalonia.Controls.Primitives; using Avalonia.Controls.Templates; using Avalonia.Data.Converters; +using Avalonia.Harfbuzz; +using Avalonia.Headless; using Avalonia.Input; using Avalonia.Platform; using Avalonia.UnitTests; @@ -195,6 +197,45 @@ namespace Avalonia.Controls.UnitTests } } + [Fact] + public void Tab_Focus_Should_Move_Focus_To_TextBox() + { + using (UnitTestApplication.Start(FocusServices)) + { + var datePicker = new CalendarDatePicker { Template = CreateTemplate() }; + var root = new TestRoot(datePicker); + root.LayoutManager.ExecuteInitialLayoutPass(); + + datePicker.Focus(NavigationMethod.Tab); + + Assert.Same(GetTextBox(datePicker), root.FocusManager.GetFocusedElement()); + } + } + + [Fact] + public void Programmatic_Focus_Should_Move_Focus_To_TextBox() + { + using (UnitTestApplication.Start(FocusServices)) + { + var datePicker = new CalendarDatePicker { Template = CreateTemplate() }; + var root = new TestRoot(datePicker); + root.LayoutManager.ExecuteInitialLayoutPass(); + + datePicker.Focus(); + + Assert.Same(GetTextBox(datePicker), root.FocusManager.GetFocusedElement()); + } + } + + private static TestServices FocusServices => TestServices.MockThreadingInterface.With( + fontManagerImpl: new HeadlessFontManagerStub(), + standardCursorFactory: Mock.Of(), + textShaperImpl: new HarfBuzzTextShaper(), + renderInterface: new HeadlessPlatformRenderInterface(), + keyboardDevice: () => new KeyboardDevice(), + keyboardNavigation: () => new KeyboardNavigationHandler(), + inputManager: new InputManager()); + private static TestServices Services => TestServices.MockThreadingInterface.With( standardCursorFactory: Mock.Of());