title: References to Controls in ContentControl are null at the Parent (Page) Level
body: <p>As a follow up to <a href="http://forums.silverlight.net/forums/t/192263.aspx" rel="nofollow noreferrer">this question</a> where it was suggested I use a ContentControl, I have run into a scenario when I use a custom made class that dervies from ContentControl on a page, any controls defined within that ContentControl are not accessible from the page. All member variables turn out null.</p>
  
  <p>For Instance, say the custom class I created that derives from <strong>ContentControl</strong> is named <strong>MyGroupBox</strong> and I try to use this inside a Page control:</p>
  
  <pre><code>&lt;UserControl x:Class="MyApplication.MyUserControl"&gt;
     &lt;local:MyGroupBox Title="Basic Information"&gt;
        &lt;TextBox x:Name="MyTextBox" /&gt;
     &lt;/local:MyGroupBox&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>When I try to access MyTextBox from within the Page's code behind, the member variable is null. What is the best workaround for this scenario to get access to these controls so that I can use them in the Page's code behind?</p>
  
  <p>Thanks!</p>
  
  <p>EDIT:
  Here is the Default Template for the MyGroupBox control</p>
  
  <pre><code>&lt;Style TargetType="local:MyGroupBox"&gt;
      &lt;Setter Property="Foreground" Value="#FF000000"/&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Left"/&gt;
      &lt;Setter Property="VerticalContentAlignment" Value="Top"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="local:MyGroupBox"&gt;
                  &lt;Border BorderThickness="1" Margin="8,8,0,0"&gt;
                      &lt;Border.BorderBrush&gt;
                          &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                              &lt;GradientStop Color="#FF979797" Offset="0"/&gt;
                              &lt;GradientStop Color="#FFF1F1F1" Offset="1"/&gt;
                          &lt;/LinearGradientBrush&gt;
                      &lt;/Border.BorderBrush&gt;
                      &lt;StackPanel&gt;
                          &lt;Grid&gt;
                              &lt;Rectangle&gt;
                                  &lt;Rectangle.Fill&gt;
                                      &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                                          &lt;GradientStop Color="White" Offset="0"/&gt;
                                          &lt;GradientStop Color="#FFDFE2ED" Offset="1"/&gt;
                                      &lt;/LinearGradientBrush&gt;
                                  &lt;/Rectangle.Fill&gt;
                              &lt;/Rectangle&gt;
                              &lt;TextBlock Text="{TemplateBinding Title}" Margin="10,3,3,3" TextAlignment="Center" HorizontalAlignment="Left" /&gt;
                          &lt;/Grid&gt;
                          &lt;ContentPresenter Cursor="{TemplateBinding Cursor}" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/&gt;
                      &lt;/StackPanel&gt;
                  &lt;/Border&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;    
  </code></pre>
  
  <p>EDIT:</p>
  
  <pre><code>public MyUserControl()
  {
      InitializeComponent();
  
      if (this.MyTextBox == null)
      {
          // MyTextBox is null at this point - is there a way to get 
          // the InitializeComponent method to find the control named MyTextBox when
          // it is inside of a ContentControl derived class?
          MessageBox.Show("MyTextBox is null");
      }
  }
  </code></pre>