title: Simple TextBox Text Binding Fails WPF
body: <p>We are converting an app from Silverlight to WPF. It's a fairly complex app, but the code sharing is about 95% +. The XAML is pretty much all the same except for XML namespace definitions etc. About 90% of the app now works but there are a few glaring issues that are puzzling me. One is this binding issue. </p>
  
  <p>We have a model object called TaskInfo. It has a property called TaskNo. in Silverlight and WPF we bind to this property like this     </p>
  
  <pre><code>&lt;TextBox IsReadOnly="True" Grid.Column="0"  Grid.Row="0" Margin="1" Text="{Binding Path=TaskNo}" Height="28"  Background="#CAECF4" VerticalAlignment="Center" VerticalContentAlignment="Center"  /&gt;
  </code></pre>
  
  <p>In both WPF and Silverlight the TaskNo is correctly displayed when the TaskInfo model is first set as the DataContext. In Silverlight, if we create a new TaskInfo, send it to the server for saving, and return the model with a new TaskNo, the TaskNo is successfully displayed. But, in WPF, it just displays 0 when the saved TaskInfo is returned from the server. There is some issue with binding. This is the binding error I see in the output window:</p>
  
  <blockquote>
    <p>System.Windows.Data Information: 10 : Cannot retrieve value using the
    binding and no valid fallback value exists; using default instead.
    BindingExpression:Path=TaskNo; DataItem=null; target element is
    'TextBlock' (Name=''); target property is 'Text' (type 'String')</p>
  </blockquote>
  
  <p>I inspected the visual tree and the TextBox's DataContext is set to the TaskInfo as expected.</p>
  
  <p>So, I turned off binding and tried this code. It's the event handler for the DataContextChanging on the TextBox. This code works fine. When a new task is saved and returned, the TaskNo successfully displays here:</p>
  
  <pre><code>    private void TaskNoBox_DataContextChanging(object sender, DependencyPropertyChangedEventArgs e)
      {
          var task = TaskNoBox.DataContext as TaskInfo;
          if (task == null)
          {
              throw new Exception("Ouch!");
          }
          TaskNoBox.Text = task.TaskNo.ToString();
      }
  </code></pre>
  
  <p>To further debug this problem, I added this event handler for the GotFocus event on the text box. So, after the task has been saved on the server side and has been returned and set as the DataContext, I click inside the control to fire this event handler. When I step through this code, I can see that the DataContext is correct, and has the correct TaskNo. Calling this code still doesn't cause the binding to occur.</p>
  
  <pre><code>    private void TextBox_GotFocus(object sender, RoutedEventArgs e)
      {
          var textBox = (TextBox)sender;
          var be = textBox.GetBindingExpression(TextBox.TextProperty);
          be.UpdateSource();
          be.UpdateTarget();
      }
  </code></pre>
  
  <p>TextBox Text binding property:
  <a href="https://i.stack.imgur.com/PPKU7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PPKU7.png" alt="enter image description here"></a></p>
  
  <p>DataContext of TextBox's properties:
  <a href="https://i.stack.imgur.com/bZ1jT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bZ1jT.png" alt="enter image description here"></a></p>
  
  <p><strong>How do I make sense of this binding error? What are the binding gotchas between Silverlight and WPF? Do I need some kind of workaround? Why is binding not working?</strong></p>