title: How do I find an element in a DataTemplate applied to a ContentPresenter?
body: <p>I've created an AddressInput control for users to enter an address. The control will have a different look depending on where it's used, so I provided a DataTemplate property called AddressTemplate. </p>
  
  <p>The default style looks like this:</p>
  
  <pre><code>&lt;Style TargetType="{x:Type addressUI:AddressInput}"&gt;
      &lt;Setter Property="AddressTemplate"
              Value="{StaticResource DefaultAddressTemplate}" /&gt;
  
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type addressUI:AddressInput}"&gt;
                  &lt;GroupBox Header="Address"&gt;
                      &lt;ContentPresenter ContentTemplate="{Binding Path=AddressTemplate, RelativeSource={RelativeSource TemplatedParent}}"
                                        Content="{Binding Path=Address, RelativeSource={RelativeSource TemplatedParent}}"
                                        x:Name="PART_AddressPresenter" /&gt;
                  &lt;/GroupBox&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>All of my address data templates will contain a combo box for selecting the country (named "PART_CountriesList"). I need to have some code-behind action that fires when the selection changes, which means I need to hook the SelectionChanged event. Inside my AddressInput I need to find PART_CountriesList in the AddressTemplate.</p>
  
  <p>I can get the "PART_AddressPresenter" ContentPresenter like this:</p>
  
  <pre><code>public override void OnApplyTemplate()
  {
      base.OnApplyTemplate();
  
      var addressPresenter = Template.FindName("PART_AddressPresenter", this) as ContentPresenter;
  }
  </code></pre>
  
  <p>Now how do I get "PART_CountriesList" contained inside AddressTemplate?</p>
  
  <p>I tried this:</p>
  
  <p>var countriesList = AddressTemplate.FindName("PART_CountriesList", addressPresenter);</p>
  
  <p>An exception is thrown because addressPresenter hasn't had its template applied yet. I know ContentPresenter has the OnApplyTemplate override, but it seems silly to extend it for this use.</p>
  
  <p>I suppose if I were to extend ContentPresenter, I'd make a new reusable version that fires an event whenever the OnApplyTemplate method executes. This would probably solve my problem, but it seems crazy. Is there a better way?</p>