When to set focus in UserControl to make it work?
See original GitHub issueHi,
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:
- Created 3 years ago
- Comments:11 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@FoggyFinder well, there also could be some property IsViewFocused in the ViewModel and DataTriggerBehavior to bind it with the same CallMethodAction.