title: Unable to access controls using VisualTreeHelper
body: <p>So, in my page I have added a Pivot. I name the Pivot <code>MainPivot</code>. Inside the <code>MainPivot</code> I've edited the <code>Title Template</code>. Inside the TitleTemplate, I've added 2 TextBlocks. One is called <code>AppTitle</code> and the other is called <code>UserText</code> </p>
  
  <p>What I want to do is set the text of both <code>AppTitle</code> and <code>UserText</code> using <code>C#</code>. To do that, I managed to get the <code>VisualTreeHelper</code> from <a href="http://www.geekchamp.com/tips/how-to-access-a-control-placed-inside-listbox-itemtemplate-in-wp7" rel="nofollow">this link</a>. Although its "How to access inside ListBox" I added a few changes to see if it worked.</p>
  
  <p>So My code.
  XAML for the edited <code>MainPivot</code>:</p>
  
  <pre><code>&lt;DataTemplate x:Key="MainPivotEditedTemplate"&gt;
   &lt;StackPanel x:Name="Stak" Orientation="Vertical" Width="0"&gt;
      &lt;TextBlock x:Name="AppTitle" HorizontalAlignment="Left" TextWrapping="NoWrap" Width="443" Margin="0,0,-443,0" FontSize="22"/&gt;
      &lt;TextBlock x:Name="UserText" HorizontalAlignment="Left" TextWrapping="NoWrap" Width="443" Margin="0,0,-443,0" FontFamily="Segoe WP SemiLight" FontSize="23"/&gt;
   &lt;/StackPanel&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>Here's the code for the <code>VisualTreeHelper</code>:</p>
  
  <pre><code>private T FindFirstElementInVisualTree&lt;T&gt;(DependencyObject parentElement) where T : DependencyObject
  {
    var count = VisualTreeHelper.GetChildrenCount(parentElement);      
  
    if (count == 0)
              return null;
  
          for (int i = 0; i &lt; count; i++)
          {
              var child = VisualTreeHelper.GetChild(parentElement, i);
  
              if (child != null &amp;&amp; child is T)
              {
                  return (T)child;
              }
              else
              {
                  var result = FindFirstElementInVisualTree&lt;T&gt;(child);
                  if (result != null)
                      return result;
              }
          }
          return null;     
  }
  </code></pre>
  
  <p>Here's the code that's meant to change the TextBlock's Texts:</p>
  
  <pre><code>public MainMenu()
  {
    InitializeComponent();
  
    Pivot apptitle = this.MainPivot.ItemContainerGenerator.ContainerFromIndex(0) as Pivot;
    Pivot usertext = this.MainPivot.ItemContainerGenerator.ContainerFromIndex(1) as Pivot;
  
    TextBlock _apptitle = FindFirstElementInVisualTree&lt;TextBlock&gt;(apptitle);
    TextBlock _usertext = FindFirstElementInVisualTree&lt;TextBlock&gt;(usertext);     
  
    _apptitle.Text = "APPLICATION TITLE";
    _usertext.Text = "USER TEXT";      
  }
  </code></pre>
  
  <p>Now my problem is that when I debug the app, it gives me an error saying:
  <code>Reference is not a valid visual DependencyObject.</code></p>
  
  <p>and shows this line in the VisualTreeHelper method:
  <code>var count = VisualTreeHelper.GetChildrenCount(parentElement);</code></p>
  
  <p>Can anyone please help me? All I want to do is access those 2 textblock's and change the text. Thanks!</p>