title: GetTemplateChild returns null when dependency property has RelativeSource binding
body: <p>I have created a custom control, <code>MyTextBox</code>, that inherits from <code>TextBox</code>. It has a style associated with it that contains a namned control:</p>
  
  <pre class="lang-xaml prettyprint-override"><code>&lt;Style x:Key="{x:Type MyTextBox}" TargetType="{x:Type MyTextBox}"&gt;
      &lt;!-- ... --&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type MyTextBox}"&gt;
                  &lt;!-- ... --&gt;
                      &lt;SomeControl x:Name="PART_SomeControl" /&gt;
                  &lt;!-- ... --&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p><code>MyTextBox</code> has a dependency property that, when set, propagates its value to <code>SomeControl</code>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MyTextBox : TextBox
  {
      // ...
  
      public static new readonly DependencyProperty MyParameterProperty =
          DependencyProperty.Register(
              "MyParameter",
              typeof(object),
              typeof(MyTextBox),
              new PropertyMetadata(default(object), MyParameterChanged));
  
      private static void MyParameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          var me = (MyTextBox)d;
          var someControl = (SomeControl)me.GetTemplateChild("PART_SomeControl");
          someControl.SetValue(SomeControl.MyParameterProperty, e.NewValue);
      }
  }
  </code></pre>
  
  <p>This works fine when doing a simple binding, like this:</p>
  
  <pre class="lang-xaml prettyprint-override"><code>&lt;MyTextBox MyParameter="{Binding}" /&gt;
  </code></pre>
  
  <p>But when I use a more fancy binding using RelativeSource, like this:</p>
  
  <pre class="lang-xaml prettyprint-override"><code>&lt;MyTextBox MyParameter="{Binding DataContext, RelativeSource={RelativeSource
      FindAncestor, AncestorType=ParentView}}"
  </code></pre>
  
  <p>the method <code>me.GetTemplateChild()</code> returns <code>null</code>. That is, <code>SomeControl</code> cannot be found.</p>
  
  <p>Why?</p>
  
  <p>One observation I have made is that, when it has a <code>RelativeSource</code>, the <code>MyParameter</code> dependency property is set first of all dependency properties. That is, if I do something like this:</p>
  
  <pre class="lang-xaml prettyprint-override"><code>&lt;MyTextBox
      OtherParameter="{Binding}"
      MyParameter="{Binding DataContext, RelativeSource={RelativeSource
          FindAncestor, AncestorType=ParentView}}"
  </code></pre>
  
  <p>the <code>MyParameter</code> property is (strangely) set before <code>OtherParameter</code>. Using the simple binding they are set in the same order as declared, just as expected.</p>
  
  <p><em>(As you can see, my code has been stripped from unrelevant stuff. Hopefully, I have included all that is important.)</em></p>