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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:04:20+00:00 2026-06-04T13:04:20+00:00

I have a usercontrol: <Grid x:Name=LayoutRoot Background=White> <TextBlock Name=TextBlock1 Text=Text for></TextBlock> </Grid> It has

  • 0

I have a usercontrol:

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock Name="TextBlock1" Text="Text for"></TextBlock> 
    </Grid>

It has a cs file:

public partial class MyUserControl: UserControl
{
    public string MyProperty { get; set; }

    public MyUserControl()
    {
        InitializeComponent();
        DoSomething();
    }

    private void DoSomething()
    {
        TextBlock1.Text = TextBlock1.Text + MyProperty;
    }
}

I’m trying to add this usercontrol programatically to another user control:

            UserControls.MyUserControl myUserControl = new UserControls.MyUserControl();
            myUserControl.MyProperty = "Something";
            MyStackPanel.Children.Add(myUserControl);

I thought I’ve done something like this before without any issues, but in this case MyProperty is always null. What did I do wrong?

  • 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-04T13:04:21+00:00Added an answer on June 4, 2026 at 1:04 pm

    Your code is fine as-is except for when you are calling DoSomething(). You should avoid GUI interaction in the constructor of a control for many reasons (not the least of which is that your property is not yet set as pointed out by Mark Hall). I do not however recommend adding different constructors just to take initial properties.

    You simply want to defer that call to the Loaded event of the control.

    Assuming you set the event in the Xaml like this:

    <navigation:Page x:Class="MyUserControl" 
        ...
        Loaded="Page_Loaded">
    

    Your code would look like:

    public partial class MyUserControl: UserControl
    {
        public string MyProperty { get; set; }
    
        public MyUserControl()
        {
            InitializeComponent();
        }
    
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
           DoSomething();
        }
    
        private void DoSomething()
        {
            TextBlock1.Text = TextBlock1.Text + MyProperty;
        }
    }
    

    If you want to set the Loaded event in code it would just look like:

    public partial class MyUserControl: UserControl
    {
        public string MyProperty { get; set; }
    
        public MyUserControl()
        {
            InitializeComponent();
            Loaded += Page_Loaded; 
        }
    
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
           DoSomething();
        }
    
        private void DoSomething()
        {
            TextBlock1.Text = TextBlock1.Text + MyProperty;
        }
    }
    

    The it is just down to you setting all your desired properties after construction, but before the page is loaded as you are doing already.

    UserControls.MyUserControl myUserControl = new UserControls.MyUserControl();
    myUserControl.MyProperty = "Something";
    MyStackPanel.Children.Add(myUserControl); // This makes it visible when Loaded event is called
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a parent usercontrol, here is an excerpt of xaml <Grid x:Name=LayoutRoot Width=Auto
I have the following XAML: <UserControl.Resources> <local:MainPageViewModel x:Key=ViewModel /> </UserControl.Resources> <Grid x:Name=LayoutRoot DataContext={Binding Source={StaticResource
I have a UserControl that has a BaseClass object as a public member. Right
The mainpage: MainPage.xaml <Canvas x:Name=LayoutRoot Background=White> </Canvas> MainPage.xaml.cs List<Usol> list = new List<Usol>(); for
I have a UserControl... XAML: <UserControl x:Name=ClientsListControl ....... > <Grid> <ListBox Margin=10 DisplayMemberPath=Name Name=profilesListBox
I have: public class Extra { public string Name {get;set;} } and I have
I have a class : public class Car { public string Name { get;
I have a simple UserControl whose direct Child element is a border:- <Border x:Name=LayoutRoot
I have a car-class like this: public class car { public String Name {get;
Using WPF and Prism. I have a view, containing 1 textblock <UserControl x:Class=ArmoryModule.Views.ResultsView xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation

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.