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

  • Home
  • SEARCH
  • 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 6017999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:12:38+00:00 2026-05-23T03:12:38+00:00

This time my test code displays a TextBox and two TextBlocks. The TextBox and

  • 0

This time my test code displays a TextBox and two TextBlocks. The TextBox and the first TextBlock are connected by two-way data binding to a string property of the data context. The second TextBlock gets updated by an event handler on the TextChanged event of the TextBox, which sets it to the current value of the string property.

Both TextBlocks are properly initialized when the window loads. But only the second one updates when I type in the TextBox. The fact that the second TextBlock is updated confirms that the two-way binding of the TextBox to the string property is working. But why doesn’t the first TextBlock get updated?

Markup:

<Window x:Class="DataBinding.SimpleDataBinding"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="SimpleDataBinding" Height="300" Width="300" >
    <StackPanel>
        <TextBox Name="txtPerson" Text="{Binding Path=Person, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <TextBlock Name="tbPerson1" Text="{Binding Path=Person, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <TextBlock Name="tbPerson2" />
    </StackPanel>
</Window>

Code behind:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace DataBinding
{
    public partial class SimpleDataBinding : Window
    {
        public string Person { get; set; }

        public SimpleDataBinding() {
             InitializeComponent();
             Person = "George";
             DataContext = this;
             txtPerson.TextChanged += new TextChangedEventHandler(txtPerson_TextChanged);
        }

        private void txtPerson_TextChanged(object sender, TextChangedEventArgs e) {
            tbPerson2.Text = Person;
        }
    }
}
  • 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-23T03:12:39+00:00Added an answer on May 23, 2026 at 3:12 am

    Your second textbox is not getting updated via databinding–it is getting updated because you are changing its value directly in the event handler.

    Your code behind needs to implement the interface INotifyPropertyChanged. This interface and implementation is the key to databinding, and if you are learning WPF/Silverlight and want to use databinding, you need to learn all about this interface and how to implement it.

    INotifyPropertyChanged

    Here is a basic implementation (from the link above):

    public event PropertyChangedEventHandler PropertyChanged;
    
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
    

    Here is how you will want to use it for your example (untested and not typed into an IDE, so may have errors):

    private string _person;
    public string Person 
    { 
        get {return _person;}
        set 
        {
            if (value == _person) return;
            _person = value;
            NotifyPropertyChanged("Person");
        }
    }
    

    Then you can get rid of this line:

    txtPerson.TextChanged += new TextChangedEventHandler(txtPerson_TextChanged);
    

    And add the binding to your second textblock:

    <TextBlock Name="tbPerson2" Text="{Binding Path=Person, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    

    You can also likely remove the names from your textbox and textblocks, as names are almost never needed when you are using databinding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Easy question this time. I'm trying to test whether or not a string does
I receive this Run-Time Check Failure upon the return in the following code. I
I've got this date time string: post[date] = 2007-07-18 10:03:19 I'd like to extract
This is similar to my previous posting. But this time I want to call
I've got a reasonably simple query (this time) that I need ALL the results
I am starting a new web application in PHP and this time around I
I'm always surprised that even after using C# for all this time now, I
I have been building enterprise software for the last 10 years. In this time
I see this from time to time and want to know what it is.
I had some time this afternoon to run a head to head comparison between

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.