title: How to access a specific item in a Listbox with DataTemplate?
body: <p>I have a ListBox including an ItemTemplate with 2 StackPanels.
  There is a TextBox in the second StackPanel i want to access. 
  (Change it's visibility to true and accept user input)
  The trigger should be the SelectionChangedEvent. So, if a user clicks on an ListBoxItem, the TextBlock gets invisible and the TextBox gets visible.</p>
  
  <p><strong>XAML CODE:</strong></p>
  
  <pre><code>&lt;ListBox Grid.Row="1" Name="ContactListBox" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ItemsSource="{Binding Contacts}" Margin="0,36,0,0" SelectionChanged="ContactListBox_SelectionChanged"&gt;
              &lt;ListBox.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;StackPanel Orientation="Horizontal" Margin="0,0,0,0"&gt;
                          &lt;toolkit:ContextMenuService.ContextMenu&gt;
                              &lt;toolkit:ContextMenu&gt;
                                  &lt;toolkit:MenuItem Header="Edit Contact" Click="ContactMenuItem_Click"/&gt;
                                  &lt;toolkit:MenuItem Header="Delete Contact" Click="ContactMenuItem_Click"/&gt;
                              &lt;/toolkit:ContextMenu&gt;
                          &lt;/toolkit:ContextMenuService.ContextMenu&gt;
  
                          &lt;Grid&gt;
                              &lt;Rectangle Fill="{StaticResource PhoneAccentBrush}"
                                             Width="72" Height="72"&gt;
                                  &lt;Rectangle.OpacityMask&gt;
                                      &lt;ImageBrush ImageSource="/Images/defaultContactImage.png" Stretch="UniformToFill"/&gt;
                                  &lt;/Rectangle.OpacityMask&gt;
                              &lt;/Rectangle&gt;
                          &lt;/Grid&gt;
                          &lt;StackPanel&gt;
                              &lt;TextBox Text="{Binding Name}" TextWrapping="Wrap" Visibility="Collapsed"/&gt;
                              &lt;TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" /&gt;
                              &lt;TextBlock Text="{Binding Number}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextAccentStyle}"/&gt;
                          &lt;/StackPanel&gt;
                      &lt;/StackPanel&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListBox.ItemTemplate&gt;
          &lt;/ListBox&gt;
  </code></pre>
  
  <p>I guess there are several ways to solve this, but nothing I tried worked. </p>
  
  <p><strong>My current approach looks like this</strong></p>
  
  <pre><code>    private void ContactListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          ListBoxItem listBoxItem = ContactListBox.SelectedItem as ListBoxItem;
  
          DataTemplate listBoxTemplate = listBoxItem.ContentTemplate;
  
          // How to access the DataTemplate content?
  
          StackPanel outerStackPanel = listBoxTemplate.XXX as StackPanel;
  
          StackPanel innerStackPanel = outerStackPanel.Children[1] as StackPanel;
  
          TextBox nameBox = innerStackPanel.Children[0] as TextBox;
          TextBlock nameBlock = innerStackPanel.Children[1] as TextBlock;
  
  
          nameBox.Visibility = System.Windows.Visibility.Visible;
          nameBlock.Visibility = System.Windows.Visibility.Collapsed;
  
      }
  </code></pre>