From 6872f53171cd59764b4dfab268eb4fd85ccd41f5 Mon Sep 17 00:00:00 2001 From: Tom Laird-McConnell Date: Fri, 13 Dec 2024 10:04:35 -0800 Subject: [PATCH] set focus on dialog --- .../View/XamlDialogWindow.axaml.cs | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Consolonia.Gallery/View/XamlDialogWindow.axaml.cs b/src/Consolonia.Gallery/View/XamlDialogWindow.axaml.cs index b253056c..23ed4b26 100644 --- a/src/Consolonia.Gallery/View/XamlDialogWindow.axaml.cs +++ b/src/Consolonia.Gallery/View/XamlDialogWindow.axaml.cs @@ -1,4 +1,6 @@ +using System.Linq; using Avalonia; +using Avalonia.Input; using Consolonia.Core.Controls; namespace Consolonia.Gallery.View @@ -8,19 +10,25 @@ public partial class XamlDialogWindow : DialogWindow public XamlDialogWindow() { InitializeComponent(); - AttachedToVisualTree += OnShowDialog; + + AttachedToVisualTree += DialogWindowAttachedToVisualTree; } - private void OnShowDialog(object sender, Avalonia.VisualTreeAttachmentEventArgs e) + private void DialogWindowAttachedToVisualTree(object sender, Avalonia.VisualTreeAttachmentEventArgs e) { - AttachedToVisualTree -= OnShowDialog; - Xaml.AttachedToVisualTree += OnButtonAttached; + AttachedToVisualTree -= DialogWindowAttachedToVisualTree; + + var child = (InputElement)this.LogicalChildren.FirstOrDefault(); + if (child != null) + child.AttachedToVisualTree += OnChildAttachedToVisualTree; } - private void OnButtonAttached(object sender, VisualTreeAttachmentEventArgs e) + private void OnChildAttachedToVisualTree(object sender, VisualTreeAttachmentEventArgs e) { - Xaml.AttachedToVisualTree -= OnButtonAttached; - Xaml.Focus(); + var child = (InputElement)sender; + child.AttachedToVisualTree -= OnChildAttachedToVisualTree; + // Set focus to the first focusable element + child?.Focus(); } } } \ No newline at end of file