question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When to set focus in UserControl to make it work?

See original GitHub issue

Hi,

I have this code for my “Connect form” and I would like to set focus to a text box after the form is displayed. My code looks like this:

using Avalonia;
using Avalonia.Controls;
using Avalonia.LogicalTree;
using Avalonia.Markup.Xaml;
using NLog;

namespace Project.AvaloniaGui.Views
{
    /// <summary>
    /// Control representing the connect form.
    /// </summary>
    public class ConnectFormView: UserControl
    {
        public ConnectFormView()
        {
            this.InitializeComponent();
        }

        protected override void OnInitialized()
        {
            base.OnInitialized();

            TextBox? host = this.FindControl<TextBox>("HostTb");
            host!.Focus();
        }

        /// <summary>
        /// Loads the object to the corresponding Avalonia component.
        /// </summary>
        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }
    }
}

Unfortunately, even though TextBox? host = this.FindControl<TextBox>("HostTb"); is not null, there is no effect of host!.Focus(); line.

Does anybody know what is wrong? I couldn’t find any examples how to work with “focus”.

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
FoggyFindercommented, Oct 10, 2020

I have tried that to no avail.

public ConnectFormView()
{
     this.InitializeComponent();
     var hostTb = this.FindControl<TextBox>("HostTb");
     if (hostTb != null)
     {
         hostTb.AttachedToVisualTree += (s,e) => hostTb.Focus();
     }
}
0reactions
maxkatz6commented, Oct 13, 2020

@FoggyFinder well, there also could be some property IsViewFocused in the ViewModel and DataTriggerBehavior to bind it with the same CallMethodAction.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I set focus to a Usercontrol
To receive focus by the TAB key just set the TabStop property to true and adjust the TabOrder property. Share.
Read more >
Can't focus User Control using Focus() - Forums
Hey, I have issue, I binded UserControl from C++ to ContentControl in WPF and after create new() UserControl I can't focus that using ......
Read more >
Wpf usercontrol load how to set focus
WPf user control load how to set foucs. What I have tried: load event use textbox.focus() . not work on page load at...
Read more >
WPF Setting focus to UserControl added at runtime
I have tried all sorts of variations of Focus() and KeyBoard. Focus to no avail. The focus remains on the clicked button of...
Read more >
How to set Focus to control in user control
Hello, You can set focus to a particular control via the Focus method. It is not necessary to change tab indexes to call...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found