title: How to get control from ItemTemplate in ItemsControl?
body: <p>My View has a collection of UserControls (defined in ItemsControl's ItemTemplate), and I want to get references to them.</p>
  
  <p>I am using <code>ItemContainerGenerator.ContainerFromIndex</code>, but it's returning <code>ContentPresenter</code>, while I would need to get my UserControl type, <code>PlotterColetaCanalUnico</code>. How should I do that?</p>
  
  <p>Xaml:</p>
  
  <pre><code>        &lt;ItemsControl x:Name="plotter" ItemsSource="{Binding Sinais}"&gt;
              &lt;ItemsControl.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;UniformGrid Columns="1" IsItemsHost="True"/&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ItemsControl.ItemsPanel&gt;
              &lt;ItemsControl.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Border x:Name="upper_light_border" BorderThickness="1,0,0,0" BorderBrush="#FFE5E5E5" SnapsToDevicePixels="True"&gt;
                          &lt;Border x:Name="lower_dark_border" BorderThickness="0,0,0,1" BorderBrush="#FF1A1A1A" SnapsToDevicePixels="True"&gt;
                              &lt;local:PlotterColetaCanalUnico/&gt;
                          &lt;/Border&gt;
                      &lt;/Border&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ItemsControl.ItemTemplate&gt;
          &lt;/ItemsControl&gt;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>    IEnumerable&lt;PlotterColetaCanalUnico&gt; SubPlotters 
      {
          get
          {
              var plotters = new List&lt;PlotterColetaCanalUnico&gt;();
  
              for(int i = 0; i &lt; plotter.Items.Count; i++)
              {
                  var container = (UIElement)plotter
                                   .ItemContainerGenerator
                                   .ContainerFromIndex(i);
  
                  // "container" ends up being ContentPresenter,
                  // so the following cast does not work!
                  var subPlotter = container as PlotterColetaCanalUnico;
  
                  if (subPlotter != null)
                  {
                      plotters.Add(subPlotter);
                  }
              }
  
              return plotters;
          }
      }
  </code></pre>
  
  <hr>
  
  <p>I got it to work based on the accepted answer, and the following changes:</p>
  
  <p>Xaml - added a name to the UserControl:</p>
  
  <pre><code>&lt;local:PlotterColetaCanalUnico x:Name="plotterCanal"/&gt;
  </code></pre>
  
  <p>Code behind - looked for the UserControl directly (without resorting to VisualTreeHelper as suggested by the answer):</p>
  
  <pre><code>                if (container == null)
                      continue;
  
                  var template = container.ContentTemplate;
                  var subPlotter = template.FindName("plotterCanal", container) as PlotterColetaCanalÚnico;
  </code></pre>