title: Why binding to an ancestor becomes active later than binding to an element by its name or binding to the DataContext?
body: <p>I noticed this while trying to set binding for a short period of time in code. In fact, I just want to get value provided by binding. So I set the binding, get value of the target property and immediately clear the binding. Everything is good until the RelativeSource with mode FindAncestor is set for the binding. In this case the target property returns its default value.</p>
  
  <p>After some debugging I discovered that the BindingExpression for the FindAncestor binding has its property Status set to Unattached. For other types of bindings BindingExpression.Status is set to Active.</p>
  
  <p>I've written some code to illustrate this.</p>
  
  <p>Window1.xaml</p>
  
  <pre><code>&lt;Window x:Class="Wpf_SetBindingInCode.Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          x:Name="Window"
          Title="Window1"
          Height="300" Width="300"
          DataContext="DataContext content"&gt;
      &lt;StackPanel&gt;
          &lt;Button Content="Set binding" Click="SetBindingButtonClick"/&gt;
          &lt;TextBlock x:Name="TextBlock1"/&gt;
          &lt;TextBlock x:Name="TextBlock2"/&gt;
          &lt;TextBlock x:Name="TextBlock3"/&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Window1.xaml.cs</p>
  
  <pre><code>public partial class Window1 : Window
  {
      public Window1()
      {
          InitializeComponent();
      }
  
      private void SetBindingButtonClick(object sender, RoutedEventArgs e)
      {
          Binding bindingToRelativeSource = new Binding("DataContext")
          {
              RelativeSource = new RelativeSource { Mode = RelativeSourceMode.FindAncestor, AncestorType = typeof(Window1) },
          };
          Binding bindingToElement = new Binding("DataContext")
          {
              ElementName = "Window"
          };
          Binding bindingToDataContext = new Binding();
  
          BindingOperations.SetBinding(TextBlock1, TextBlock.TextProperty, bindingToRelativeSource);
          BindingOperations.SetBinding(TextBlock2, TextBlock.TextProperty, bindingToElement);
          BindingOperations.SetBinding(TextBlock3, TextBlock.TextProperty, bindingToDataContext);
  
          Trace.WriteLine("TextBlock1.Text = \"" + TextBlock1.Text + "\"");
          Trace.WriteLine("TextBlock2.Text = \"" + TextBlock2.Text + "\"");
          Trace.WriteLine("TextBlock3.Text = \"" + TextBlock3.Text + "\"");
  
          var bindingExpressionBase1 = BindingOperations.GetBindingExpressionBase(TextBlock1, TextBlock.TextProperty);
          var bindingExpressionBase2 = BindingOperations.GetBindingExpressionBase(TextBlock2, TextBlock.TextProperty);
          var bindingExpressionBase3 = BindingOperations.GetBindingExpressionBase(TextBlock3, TextBlock.TextProperty);
  
          Trace.WriteLine("bindingExpressionBase1.Status = " + bindingExpressionBase1.Status);
          Trace.WriteLine("bindingExpressionBase2.Status = " + bindingExpressionBase2.Status);
          Trace.WriteLine("bindingExpressionBase3.Status = " + bindingExpressionBase3.Status);
      }
  }
  </code></pre>
  
  <p>The code above produces the following output:</p>
  
  <pre><code>TextBlock1.Text = ""
  TextBlock2.Text = "DataContext content"
  TextBlock3.Text = "DataContext content"
  bindingExpressionBase1.Status = Unattached
  bindingExpressionBase2.Status = Active
  bindingExpressionBase3.Status = Active
  </code></pre>
  
  <p>But despite this all three TextBlocks on the form has expected values - "DataContext content".</p>
  
  <p>So my questions are:</p>
  
  <ol>
  <li><p>Why the RelativeSourceMode.FindAncestor binding does not provide the
  value immediately after BindingOperations.SetBinding(...) is called?</p></li>
  <li><p>Is there any way to force this kind of binding to update the target
  property? I tried to call bindingExpression.UpdateTarget() - it
  doesn't work like expected.</p></li>
  </ol>