Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8412077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:35:09+00:00 2026-06-10T00:35:09+00:00

My main window has a content control that I load a user control into.

  • 0

My main window has a content control that I load a user control into. I can’t seen to set the focus to a text box in the user control. I’ve tried several suggested methods like textBox1.Focus() and Keyboard.Focus() and FocusManager.SetFocusedElement() but they don’t seem to work.

I’m not tied to this way of loading the user control if there is a better way. It seems like I’m missing something pretty fundamental regarding either focus or the ContentControl so I thought I’d ask the experts instead of continuing to shot gun the problem.

Any ideas?

MainWindow.xaml:

<Grid>
    <ContentControl Name="NavFrame"/>
</Grid>

MainWindow.xaml.cs:

protected override void OnInitialized(EventArgs e)
{
    this.NavFrame.Content = new UserControl1();
    base.OnInitialized(e);
}

UserControl1.xaml:

<Grid>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="93,79,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
</Grid>

UserControl1.xaml.cs:

protected override void OnInitialized(EventArgs e)
{
    textBox1.Focus();
    // FocusManager.SetFocusedElement(this, textBox1);
    // Keyboard.Focus(textBox1);
    base.OnInitialized(e);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:35:10+00:00Added an answer on June 10, 2026 at 12:35 am

    If you put the textBox1.Focus() into the loaded event for the user control rather than OnInitialized it seems to work correctly.

    <UserControl ...
        Loaded="UserControl_Loaded">
        <Grid>
            <TextBox Name="textBox1" />
        </Grid>
    </UserControl>
    

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        textBox1.Focus();
    }
    

    Something as simple as this works as well, if you want to give focus on times other than OnLoad…

    UserControl1.xaml.cs:

    public void FireTextBoxFocus()
    {
        textBox1.Focus();
    }
    

    MainWindow.xaml.cs:

    UserControl1 ctrl = new UserControl1();
    ContentControl1.Content = ctrl;
    ctrl.FireTextBoxFocus();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a WPF application the main window has a content control to which one
I have a user control that often has 2000 or so component controls (TextBoxes,
I'm trying to create a user control that has a Grid with two rows.
I am working on an app whose main window has an NSDrawer. It is
I want to create an application which main window has canvas (or something where
I have main window which has inner grid components. When I press a button
My swing app has a main window which has sever buttons. This JFrame is
I have a window(say main window) with a frame which has a page in
I have a main window, that opens another window. I want to close this
My application has a restricted access. I have a user/password box in a small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.