Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set focus on a control when Window is loaded with WinUI 3

I want to set focus to a textbox user control when the window is loaded.

I’ve tried putting txtLogin.Focus(FocusState.Programmatic); in the constructor

I’ve also tried doing something like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


    void MainWindow_Activated(object sender, WindowActivatedEventArgs e)
    {
      txtLogin.Focus(FocusState.Programmatic);
    }

What am I missing?

>Solution :

Try the Loaded event:

public MainWindow()
{
    this.InitializeComponent();

    this.txtLogin.Loaded += (s, e) =>
    {
        this.txtLogin.Focus(FocusState.Programmatic);
    };
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading