howard39
howard39

Reputation: 25

Another WPF Binding Syntax Question

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;
        }
    }
}

Upvotes: 0

Views: 117

Answers (1)

Phil Sandler
Phil Sandler

Reputation: 28016

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.

Upvotes: 1

Related Questions