From d21c3cce1ef1a38037d589aad9d126a36b17df11 Mon Sep 17 00:00:00 2001 From: Tim U Date: Wed, 5 Jan 2022 11:17:38 +0100 Subject: [PATCH] Improve Demo - Set the focus to the TextBox of the NumericUpDown --- samples/ControlCatalog/Pages/DataGridPage.xaml | 2 +- .../ControlCatalog/Pages/DataGridPage.xaml.cs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/samples/ControlCatalog/Pages/DataGridPage.xaml b/samples/ControlCatalog/Pages/DataGridPage.xaml index 451a774cb4..f7e3cf2441 100644 --- a/samples/ControlCatalog/Pages/DataGridPage.xaml +++ b/samples/ControlCatalog/Pages/DataGridPage.xaml @@ -72,7 +72,7 @@ - + diff --git a/samples/ControlCatalog/Pages/DataGridPage.xaml.cs b/samples/ControlCatalog/Pages/DataGridPage.xaml.cs index ec2c6651de..c010d38eac 100644 --- a/samples/ControlCatalog/Pages/DataGridPage.xaml.cs +++ b/samples/ControlCatalog/Pages/DataGridPage.xaml.cs @@ -6,7 +6,9 @@ using Avalonia.Controls; using Avalonia.Markup.Xaml; using ControlCatalog.Models; using Avalonia.Collections; +using Avalonia.Controls.Primitives; using Avalonia.Data; +using Avalonia.Threading; namespace ControlCatalog.Pages { @@ -84,5 +86,19 @@ namespace ControlCatalog.Pages return Comparer.Default.Compare(x, y); } } + + private void NumericUpDown_OnTemplateApplied(object sender, TemplateAppliedEventArgs e) + { + // We want to focus the TextBox of the NumericUpDown. To do so we search for this control when the template + // is applied, but we postpone the action until the control is actually loaded. + if (e.NameScope.Find("PART_TextBox") is {} textBox) + { + Dispatcher.UIThread.InvokeAsync(() => + { + textBox.Focus(); + textBox.SelectAll(); + }, DispatcherPriority.Loaded); + } + } } }