views:

6844

answers:

2

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.)

+1  A: 

We might need to see the code. Does your string property raise a PropertyChanged event? Or (even better) is it implemented as a DependencyProperty? If not, the bound TextBox won't know when the value changes.

As for typing into the TextBox and not seeing the property's value change, that may be because your TextBox isn't losing focus. By default, bound TextBoxes don't write their values back to the source property until focus leaves the control. Try tabbing out of it and seeing if the property value changes.

Matt Hamilton
+13  A: 

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
}
Gishu
+1 In my experience this is the most likely problem.
Bryan Anderson
This helped point me in the right direction. I'm not going to give you check-mark, though, because I ended up doing a lot of research on my own (guided by your answer) before I figured it out. If you add more detail (and maybe some simple code snippets), you'll get the check mark.
evizaer
I would argue that the post which points you in the most productive direction is as worthy of a checkmark as a direct answer, if not more.
Superstringcheese
I think Yoda updated your source and changed the null check :)
Chris S