Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 298
evizaer Avatar asked Nov 26 '08 08:11

evizaer


People also ask

What is 2 way binding in WPF?

Two way binding is used when we want to update some controls property when some other related controls property change and when source property change the actual control also updates its property.

What is two way data binding C#?

Two-way data binding binds an input element in an HTML block to a field or property in a C# @code block and vice versa. The input element displays the value of the field or property. Conversely, changing the value of the input element in the HTML block changes the field value or property value in the C# @code block.

What is binding mode in WPF?

Default Data-bindingIt just defines which is the default binding mode for the control's property. In WPF different controls has different default data-binding modes. For example, TextBlock's Text property has one-way as default binding mode but a TextBox's Text property has a two-way binding mode.

What is 2way binding?

Two-way binding gives components in your application a way to share data. Use two-way binding to listen for events and update values simultaneously between parent and child components.


2 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 } 
like image 81
Gishu Avatar answered Sep 22 '22 18:09

Gishu


I feel the need to add some precision:

"Two ways" data binding is more than "One way" data binding.

"One way" data binding is a binding from a source to a dependency property. The source must implement INotifyPropertyChanged, in order to get change propagation from source to target.

To get the " 2 way" , so to get a propagation from Target to Source, it depends on the binding mode which you set on the Binding . If you don't set any BindingMode for your binding, the default Binding mode will be used, and this default mode is a characteristics for your target Dependency Property.

Example:

A Textbox bound to a string property, called "MyTextProperty". In the code, you bind Textbox.Text DependencyProperty to "MyTextProperty" on object "MyObject"

--> "one way" binding : the setter of "My TextProperty" must raise an event Property Changed,and "MyObject" must implement INotifyPropertyChanged.

--> "2 ways data binding": in addition to what is needed for "One way", bindingMode must be set to "2 ways". In this special case, the Text DependencyProperty for Textbox does have "2 ways" as default mode, so there is nothing else to do !

like image 24
flamandier Avatar answered Sep 20 '22 18:09

flamandier