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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:41:00+00:00 2026-05-16T15:41:00+00:00

This has to be simple, at least it was in good old .Net where

  • 0

This has to be simple, at least it was in good old .Net where it took maybe four lines of code. I’m working in VS2010, C#, WPF4.

I have a user control with a textbox. When I click a button in the main window, I want my user control textbox to reflect some text. Is this possible in WPF4 with less than 500 lines of esoteric code?

The problem is that while I know the textbox is getting the new text as evidenced from breakpoints in the user control code, that text is never being reflected to the main window. The main window still shows the original text. It has to be some kind of binding thing, and I really don’t think I should have to create templates and resources and all for this simple situation. It’s got to be something simple that I’m forgetting in the forest of WPF4. Below is what I have. After clicking the button, the textbox is still blank; it does not say “hello earthlings.”

In the user control code:

public partial class UserControl1 : UserControl
{
    public static readonly DependencyProperty TextProperty;

    public UserControl1()
    {
        InitializeComponent();
    }

    static UserControl1()
    {
        TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(UserControl1), new UIPropertyMetadata(null));
    }

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

}

User control xaml:

<UserControl x:Class="WTFUserControlLibrary.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Grid Height="164" Width="220">
    <TextBox Name="txtTest" BorderBrush="red" BorderThickness="2" Height="25" Text="{Binding ElementName=UserControl1, Path=Text, Mode=TwoWay}"></TextBox>
</Grid>

(I have no idea what the text binding is supposed to be doing in this case.)

Main window code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        WTFUserControlLibrary.UserControl1 uc = new WTFUserControlLibrary.UserControl1();
        uc.Text = "hello earthlings";
    }
}

and the main window xaml:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:WTFUserControlLibrary;assembly=WTFUserControlLibrary"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="71,65,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <my:UserControl1 HorizontalAlignment="Left" Margin="71,94,0,0" Name="userControl11" VerticalAlignment="Top" Height="116" Width="244" />
</Grid>

Thanks Earthlings (and also those who designed this mess!)

  • 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-16T15:41:01+00:00Added an answer on May 16, 2026 at 3:41 pm

    In your method button1_Click you are creating a new user control. This is not the usercontrol in the window and is never displayed.

    Instead, give your usercontrol a name in the XAML:

    x:Name="uc"
    

    Then in the button1_Click method you just remove that first line where you create a new usercontrol.

    update

    You want the user control XAML to look more like this:

    <UserControl x:Class="WTFUserControlLibrary.UserControl1"
             x:Name="thisControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
    <Grid Height="164" Width="220">
        <TextBox Name="txtTest" BorderBrush="red" 
                 BorderThickness="2" Height="25" 
                 Text="{Binding ElementName=thisControl, Path=Text, Mode=TwoWay}" />
    </Grid>
    

    I added x:Name="thisControl" to the root UserControl element, and then referenced this in the binding.

    I’ll try to explain the binding:

    You have this textbox inside your user control, but you want to be able to bind the text value to something outside the user control. What you’ve done is set up a dependency property on the user control, and bound that textbox to it, so you are using the binding infrastructure pass values from the top level of the UserControl to constituent controls inside it.

    Basically, it looks like this:

    data
        ---> bind UserControl1.Text to data
             ---> bind TextBox.Text to UserControl1.Text
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This probably has a simple answer, but I must not have had enough coffee
This has got to be something simple: I set up a frames page with
Ok, this probably has a really simple answer, but I've never tried to do
Ok, this has got to be a super simple problem. I just can't seem
This really has my stumped today. I'm sure its simple, but... Here is my
There has to be a simple explanation for this. I have a parent <div>
Alright, this one (3a; sample problem with provided answer) has got me scratching my
This has been a problem that I haven't been able to figure out for
This has been an adventure. I started with the looping duplicate query located in
This has been driving me crazy for a few days. Why doesn't the following

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.