title: Find Silverlight TreeViewItem control by Header
body: <p>I am trying to create a TreeView from the Silverlight TreeView control.  I have my data being pulled from a WCF service that pulls from EF.  All of the data is coming in fine.  I have the page set up where I can input a UserName, click a button, and the data will populate the first generation in the TreeView.  So, I'm dynamically building TreeViewItems to put into my TreeView with a Selected RoutedEventHandlers attached to each one.  When I click on one of the TreeViewItem nodes, it kicks off the tvi_Selected function in which I want to populate TreeViewItems under the TreeViewItem that I just selected.</p>
  
  <p>I run into problem when I am in my delegate function prox_GetChildMembersCompleted.  I can't figure out a way to do a FindControl type lookup on the TreeViewItem that I want to add the child TreeViewItem elements to. So, I thought that I would just create a protected field where I would store the Header information to because it contain only the UserName.  I just need to be able to access a specific TreeViewItem by Header or some other method that is alluding me.</p>
  
  <p>You can see that in my Selected eventhandler, that I am getting the Header info by casting the sender object to a TreeViewItem.  In the the delegate function prox_GetChildMembersCompleted that is called inside of tvi_Selected, the sender object is WCFDataClient so I can't grab the same data from that sender.  Any insight into this would be much appreciated even if you suggest a method that is completely different.</p>
  
  <pre><code>&lt;UserControl xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"  
      xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
      x:Class="FloLOS2.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"&gt;
    &lt;Grid x:Name="LayoutRoot" Background="#5C7590"&gt;
          &lt;StackPanel&gt;
              &lt;TextBox x:Name="txtUserName" Width="120" Margin="5"&gt;&lt;/TextBox&gt;
              &lt;TextBlock x:Name="txtFillBlock" Width="300" Margin="5" Foreground="White" Text="Change me"&gt;&lt;/TextBlock&gt; 
              &lt;Button x:Name="btnSubmit" Margin="5" Content="Get Frontline" Width="120" Click="btnSubmit_Click" /&gt;
              &lt;data:DataGrid x:Name="MembersGrid" Margin="5"&gt;&lt;/data:DataGrid&gt;
              &lt;controls:TreeView x:Name="MembersTree" Margin="5"&gt;&lt;/controls:TreeView&gt;
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  
  
  
  namespace FloLOS2
  {
      public partial class MainPage : UserControl
      {
          string sParentID;
  
          public MainPage()
          {
              InitializeComponent();
          }
  
          private void btnSubmit_Click(object sender, RoutedEventArgs e)
          {
              GetMyDataRef.GetMyDataClient prox = new FloLOS2.GetMyDataRef.GetMyDataClient();
              prox.GetMembersCompleted += new EventHandler&lt;FloLOS2.GetMyDataRef.GetMembersCompletedEventArgs&gt;(prox_GetMembersCompleted);
              prox.GetMembersAsync(txtUserName.Text);
          }
  
          void prox_GetMembersCompleted(object sender, FloLOS2.GetMyDataRef.GetMembersCompletedEventArgs e)
          {
              GetMyDataRef.Member[] members = e.Result.ToArray();
  
              foreach (var x in members)
              {
                  TreeViewItem tvi = new TreeViewItem() { Header = x.UserName };
                  tvi.Selected += new RoutedEventHandler(tvi_Selected);
                  MembersTree.Items.Add(tvi);
              }
  
              //MembersTree.Items.Add(tvi);
          }
  
          void prox_GetChildMembersCompleted(object sender, FloLOS2.GetMyDataRef.GetMembersCompletedEventArgs e)
          {
              GetMyDataRef.Member[] members = e.Result.ToArray();
              TreeViewItem tviParent = new TreeViewItem();
              // *** Find TreeViewItem control based on Header  ***
  
              foreach (var x in members)
              {
                  TreeViewItem tviChild = new TreeViewItem() { Header = x.UserName };
                  tviChild.Selected += new RoutedEventHandler(tvi_Selected);
                  tviParent.Items.Add(tviChild);
              }
  
          }
  
          void tvi_Selected(object sender, RoutedEventArgs e)
          {
              try
              {
  
                  TreeViewItem item = (TreeViewItem)sender;
                  txtFillBlock.Text = item.Header.ToString();
                  sParentID = item.Header.ToString();
  
                  GetMyDataRef.GetMyDataClient prox = new FloLOS2.GetMyDataRef.GetMyDataClient();
                  prox.GetMembersCompleted += new EventHandler&lt;FloLOS2.GetMyDataRef.GetMembersCompletedEventArgs&gt;(prox_GetChildMembersCompleted);
                  prox.GetMembersAsync(item.Header.ToString());
  
              }
              catch (Exception ex)
              {
                  txtFillBlock.Text = ex.InnerException.ToString();
              }
          }
  
  
      }
  }
  </code></pre>