Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp with some very basic WPF databinding
    text
    copied!<p>I'm new to WPF and trying a simple example of databinding, but it's not working. My window has a TextBlock, which I bound to a property of the window object. I declared the property in code. </p> <p>When running this, I see the correct value appearing in the TextBlock. There's also a button, which when clicked updates the property, except I don't see this affecting the TextBlock.</p> <p>I implemented the INotifyPropertyChanged correctly, as far as I'm able to determine. I also see, when debugging, that <em>something</em> has subscribed to the PropertyChanged event, except it doesn't seem to do anything.</p> <p>I have 2 questions:</p> <p>1) Why isn't this working as expected?</p> <p>2) Is there any easy way to debug during run-time what's causing this, without resorting to third-party tools? From my cursory knowledge, it seems to me the debugging support in WPF is sorely lacking.</p> <p>The XAML is (not including the "standard" XAML window element):</p> <pre><code>&lt;TextBlock Height="28" Name="label1" VerticalAlignment="Top" Text="{Binding Path=TheName}" Grid.Row="0" &gt;&lt;/TextBlock&gt; &lt;Button Height="23" Name="button1" VerticalAlignment="Stretch" Grid.Row="1" Click="button1_Click"&gt; Button &lt;/Button&gt; </code></pre> <p>The code in the window class is:</p> <pre><code> public partial class Window1 : Window { protected MyDataSource TheSource { get; set; } public Window1() { InitializeComponent(); TheSource = new MyDataSource(); TheSource.TheName = "Original"; // This works this.label1.DataContext = this.TheSource; } private void button1_Click(object sender, RoutedEventArgs e) { TheSource.TheName = "Changed"; // This doesn't work } } public class MyDataSource : INotifyPropertyChanged { string thename; public string TheName { get { return thename; } set { thename = value; OnPropertyChanged(thename); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } } } </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