Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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).<br> For normal CLR property, you'd need to implement INotifyPropertyChanged and force update on the textbox in the event handler for PropertyChanged. </p> <ul> <li>So make your object with the property implement this interface, raise the event in the property setter. (So now we have property change notification)</li> <li>Make sure the object is set as the DataContext property of the UI element/control</li> </ul> <p>This threw me off too when I started learning about WPF data binding.</p> <p><strong>Update:</strong> <em>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.</em> 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.</p> <p>MyWindow.xaml</p> <pre><code>&lt;Window x:Class="DataBinding.MyWindow" ... Title="MyWindow" Height="300" Width="300"&gt; &lt;StackPanel x:Name="TopLevelContainer"&gt; &lt;TextBox x:Name="txtValue" Background="AliceBlue" Text="{Binding Path=MyDotNetProperty}" /&gt; &lt;TextBlock TextWrapping="Wrap"&gt;We're twin blue boxes bound to the same property.&lt;/TextBlock&gt; &lt;TextBox x:Name="txtValue2" Background="AliceBlue" Text="{Binding Path=MyDotNetProperty}" /&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>MyWindow.xaml.cs</p> <pre><code>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 } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload