title: XAML binding from a string to TextBox not working
body: <p>I'm a C# newbie who's having problems understanding why his first attempt at understanding XAML bindings isn't working. I'm following Microsoft's <a href="http://msdn.microsoft.com/en-us/library/ms752347.aspx" rel="nofollow">Data Binding Overview</a>.</p>
  
  <p>I have a single TextBox which will eventually serve as a status window. For now I just want to be able to write arbitrary text strings into it. I also have an instance of the command pattern that I'm testing. My command involves adding a random number to an accumulator and printing out the result to the status view.</p>
  
  <p>Here's my XAML for the main application window:</p>
  
  <pre><code>&lt;Window x:Class="Experiment.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:c="clr-namespace:Experiment"
          Title="Test" Height="500" Width="700" Name="Test" Closing="Test_Closing" DataContext="{Binding ElementName=statusText}" xmlns:my="clr-namespace:Experiment"&gt;
      &lt;Window.Resources&gt;
          &lt;c:StatusViewText x:Key="statusViewText" /&gt;
      &lt;/Window.Resources&gt;
      &lt;DockPanel HorizontalAlignment="Stretch" Margin="0,0,0,0" Width="Auto"&gt;
          &lt;!-- Elements deleted for brevity. --&gt;
          &lt;TextBox Margin="5,5,5,5" Name="statusText" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" AcceptsReturn="True" AcceptsTab="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" FontFamily="Courier New" FontSize="12"
                   Text="{Binding Source={StaticResource statusViewText}, Path=statusTextString, Mode=OneWay}"/&gt;
      &lt;/DockPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>And the class representing my data:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace Experiment {
      public class StatusViewText {
          public StringBuilder statusText { get; set; }
          public String statusTextString { get; set; }
  
          public StatusViewText() {
              statusText = new StringBuilder();
          }
  
          public void Append(string s) {
              if (s != null) {
                  statusText.Append(s);
              }
  
              statusTextString = statusText.ToString();
          }
  
          public void AppendLine(string s) {
              if (s != null) {
                  statusText.AppendLine(s);
              }
  
              statusTextString = statusText.ToString();
          }
      }
  }
  </code></pre>
  
  <p>Eventually I'll use a proper converter here from StringBuilder, but I wanted to get the principle down before exploring that complexity.</p>
  
  <p>If my understanding were correct (and obviously it isn't), this all SHOULD work. The binding target is the TextBox, target property is the Text property. The binding source is the StatusViewText class's statusTextString property. Yet when I run the command (and debug and see StatusViewText.statusTextString being updated), the TextBox doesn't update.</p>
  
  <p>I thought that I may need to explicitly add the binding myself, so I tried adding this after <code>InitializeComponent()</code> in the main window constructor:</p>
  
  <pre><code>        statusViewText = new StatusViewText();
          Binding statusViewTextBinding = new Binding("statusTextString");
          statusViewTextBinding.Source = statusViewText;
          statusText.SetBinding(TextBlock.TextProperty, statusViewTextBinding);
  </code></pre>
  
  <p>but that didn't work either. </p>
  
  <p>Do I need to fire PropertyChanged events? I thought the entire point of the binding framework is that events are fired and consumed behind the scenes, automagically; but maybe I'm wrong there too.</p>
  
  <p>No obvious errors are coming up in the Output window which leads me to believe that my binding syntax is correct; I'm just missing something else.</p>
  
  <p>Halp!</p>
  
  <p><strong>EDIT 13:14 EDT</strong> Thanks <strong>mben</strong>:</p>
  
  <p>Ok, I did that. Added the following:</p>
  
  <pre><code>public class StatusViewText : INotifyPropertyChanged {
      public void Append(string s) {
          if (s != null) {
              statusText.Append(s);
          }
  
          statusTextString = statusText.ToString();
          NotifyPropertyChanged("statusTextString");
      }
  
      public void AppendLine(string s) {
          if (s != null) {
              statusText.AppendLine(s);
          }
  
          statusTextString = statusText.ToString();
          NotifyPropertyChanged("statusTextString");
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      private void NotifyPropertyChanged(String info) {
          if (PropertyChanged != null) {
              PropertyChanged(this, new PropertyChangedEventArgs(info));
          }
      }
  }
  </code></pre>
  
  <p>I debugged to verify that it was going through this code path, and it is. But still no luck. Any other thoughts?</p>