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 7070869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:37:06+00:00 2026-05-28T05:37:06+00:00

I created a UserControl that contains a textbox . When I try to initialize

  • 0

I created a UserControl that contains a textbox. When I try to initialize the textbox in the constructor of the UserControl, with some text like this

public FileSelector()
{
    InitializeComponent();
    TB_FolderPath.Text = @"c:\tmp\Test\";
}

I get an error in the MainWindow.xaml

Cannot create an instance of “FileSelector”.

When I remove the row

TB_FolderPath.Text = @"c:\tmp\Test\";

I don’t get the error, but of course an empty textbox.

Previously when I had the parts of the UserControl integrated in the MainWindow, there was also no problem.

I tried to create a simpler version of MainWindow using a UserControl to reproduce the problem, but in a simple case it works.

So, my questions.

  • What can be the cause of the problem?

  • How can I debug/analyze a problem like this systematically? I just get the this error in VisualStudio after building, without an explanation.

  • How/Where can I initialize the controls in a UserControl. In general, is the UserControl the right place to initialize the controls or would the MainWindow be also a possibility? (Is this possible at all?)

  • 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-05-28T05:37:07+00:00Added an answer on May 28, 2026 at 5:37 am

    In WPF, unlike WinForms, the controls are not initialized completely after InitializeComponent(). Hence the uninitialized/unloaded controls throw errors.

    You need to write handler to capture the Loaded event of the control.

    Read Object Lifetime Events.

    Get some more detailed info here.

    Example (partially taken from OP’s code):

    public FileSelector()
    {
        InitializeComponent();
        TB_FolderPath.Loaded += delegate { TB_FolderPath.Text = @"c:\tmp\Test\"; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can remember that some time ago when I created a UserControl in Visual
I created a AutoCompleteExtender on a TextBox that resides on a UserControl (Control.ascx file).
I have created a user control that contains a button. I am using this
I am using MVVM Light. I have created a window that looks like this:
I have a UserControl that contains a CheckBox and a TextBox: <asp:CheckBox runat=server ID=chk1
I have created a UserControl that contains an Ellipse and a few lines. I
I've created a UserControl that contains a ScrollViewer panel that contains a data bounds
Ive created a custom UserControl that contains a single combobox. The currently selected value
I have a UserControl that contains an UpdatePanel which wraps some other controls. The
I've created a user control (in vb.net code) that contains two dock panels, one

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.