title: Access DataTemplate controls in code behind
body: <p>I have problem with this code:</p>
  
  <pre><code>&lt;ListBox x:Name="lbInvoice" ItemsSource="{Binding ocItemsinInvoice}"&gt;
  &lt;ListBox.ItemTemplate&gt;
      &lt;DataTemplate&gt;
          &lt;StackPanel&gt;
              &lt;ToggleButton x:Name="btnInvoiceItem"&gt;
                  &lt;StackPanel Orientation="Horizontal"&gt;
                      &lt;ToggleButton x:Name="btnInvoiceQuantity" Content="{Binding Quantity}"/&gt;
                      &lt;TextBlock Text="{Binding Item.ItemName}" Width="175" Padding="7,5,0,0"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/ToggleButton&gt;
              &lt;Popup x:Name="popQuantity" Closed="popQuantity_Closed" PlacementTarget="{Binding ElementName=btnInvoiceQuantity}" IsOpen="{Binding IsChecked,ElementName=btnInvoiceQuantity}"&gt;
                      &lt;Grid&gt;
                          &lt;TextBlock x:Name="tbUnitPrice" Text="Unit Price"/&gt;
                          &lt;Button x:Name="btnClosePopup" Click="btnClosePopup_Click"&gt;
                      &lt;/Grid&gt;
              &lt;/Popup&gt;
          &lt;/StackPanel&gt;
      &lt;/DataTemplate&gt;
  &lt;/ListBox.ItemTemplate&gt;
  </code></pre>
  
  <p></p>
  
  <p>In code behind in <code>btnClosePopup</code> click event I can't access to popup to close it and do some other changes on it.</p>
  
  <p>I have tried to use <code>FindName()</code> method but it doesn't work for me</p>
  
  <pre><code>var template = lbInvoice.Template;
  var myControl = (Popup)template.FindName("popQuantity", lbInvoice);
  </code></pre>
  
  <p>Please can you help and tell me how do I access to controls that inside DataTemplate in code behind?</p>