Two-way binding in WPF

I cannot get a two-way bind in WPF to work. I have a string property in my app's main window that is bound to a textbox (I set the mode to "TwoWay"). The only time that the value of the textbox will update is when the window initializes. When I type into the textbox, the underlying string properties value does not change. When the string property's value is changed by an external source (an event on Click, for example, that just resets the textbox's value), the change doesn't propagate up to the textbox.

What are the steps that I must implement to get two-way binding to work properly in even this almost trivial example?

(I can provide code if it's really necessary. I'm more looking for the process. I understand the basics of data binding, though I'm very new to WPF.)

Answers


Most probably you're trying to bind to a .net CLR property instead of a WPF dependencyProperty (which provides Change Notification in addition to some other things). For normal CLR property, you'd need to implement INotifyPropertyChanged and force update on the textbox in the event handler for PropertyChanged.

  • So make your object with the property implement this interface, raise the event in the property setter. (So now we have property change notification)
  • Make sure the object is set as the DataContext property of the UI element/control

This threw me off too when I started learning about WPF data binding.

Update: Well OP, it would have been a waste of time if i was barking up the wrong tree.. anyways now since you had to dig a bit.. you'll remember it for a long time. Here's the code snippet to round off this answer. Also found that updating the textbox happens automatically as soon as I tab-out.. You only need to manually subscribe to the event and update the UI if your datacontext object is not the one implementing INotifyPropertyChanged.

MyWindow.xaml

<Window x:Class="DataBinding.MyWindow" ...
    Title="MyWindow" Height="300" Width="300">
    <StackPanel x:Name="TopLevelContainer">
        <TextBox x:Name="txtValue"  Background="AliceBlue" Text="{Binding Path=MyDotNetProperty}" />
        <TextBlock TextWrapping="Wrap">We're twin blue boxes bound to the same property.</TextBlock>
        <TextBox x:Name="txtValue2"  Background="AliceBlue" Text="{Binding Path=MyDotNetProperty}" />
    </StackPanel>
</Window>

MyWindow.xaml.cs

public partial class MyWindow : Window, INotifyPropertyChanged
{
    public MyWindow()
    {
        InitializeComponent();
        this.MyDotNetProperty = "Go ahead. Change my value.";
        TopLevelContainer.DataContext = this;
    }

    private string m_sValue;
    public string MyDotNetProperty
    {
        get { return m_sValue; }
        set
        {
            m_sValue = value;
            if (null != this.PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("MyDotNetProperty"));
            }
        }
    }

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion
}

Need Your Help

XML test scripts / runners

xml testing qa

Is there any xml testing frameworks out there that would allow me to write scripts for testing/asserting contents of an xml file??

how alert make javascript interrupt? can I do it with code?

javascript jquery

I'm procesing a kind of "big" JSON object around of 4000 elements passing for different methods, and I would like to update a div tag with a text showing the process.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.