diff --git a/Xwt.WPF/Xwt.WPFBackend/LabelBackend.cs b/Xwt.WPF/Xwt.WPFBackend/LabelBackend.cs index 4b1dfb54..94d61b5c 100644 --- a/Xwt.WPF/Xwt.WPFBackend/LabelBackend.cs +++ b/Xwt.WPF/Xwt.WPFBackend/LabelBackend.cs @@ -35,6 +35,7 @@ using SWD = System.Windows.Documents; using Xwt.Backends; using System.Windows.Automation.Peers; +using System.Windows.Threading; namespace Xwt.WPFBackend { @@ -82,6 +83,29 @@ namespace Xwt.WPFBackend } } + void FocusOnUIThread () + { + // Using Render (7) priority here instead of default Normal (9) so that + // the component has some time to initialize and get ready to receive the focus + Widget.Dispatcher.BeginInvoke ((Action) (() => { + ((WpfLabel) Widget).TextBlock.Focus (); + }), DispatcherPriority.Render); + } + + public new void SetFocus () + { + if (Widget.IsLoaded) + FocusOnUIThread (); + else + Widget.Loaded += DeferredFocus; + } + + void DeferredFocus (object sender, RoutedEventArgs e) + { + Widget.Loaded -= DeferredFocus; + FocusOnUIThread (); + } + public bool Selectable { get; set; } // TODO: this is only supported on Win10 with UWP? public void SetFormattedText (FormattedText text) diff --git a/Xwt.WPF/Xwt.WPFBackend/WidgetBackend.cs b/Xwt.WPF/Xwt.WPFBackend/WidgetBackend.cs index eefce69a..ba711304 100644 --- a/Xwt.WPF/Xwt.WPFBackend/WidgetBackend.cs +++ b/Xwt.WPF/Xwt.WPFBackend/WidgetBackend.cs @@ -270,17 +270,18 @@ namespace Xwt.WPFBackend void FocusOnUIThread () { - Widget.Dispatcher.BeginInvoke ((Action) (() => - { + // Using Render (7) priority here instead of default Normal (9) so that + // the component has some time to initialize and get ready to receive the focus + Widget.Dispatcher.BeginInvoke ((Action) (() => { Widget.Focus (); }), SW.Threading.DispatcherPriority.Render); } public void SetFocus () { - if (Widget.IsLoaded) { + if (Widget.IsLoaded) FocusOnUIThread (); - } else + else Widget.Loaded += DeferredFocus; }