title: Finding ALL child controls WPF
body: <p>I would like to find all of the controls within a WPF control. I have had a look at a lot of samples and it seems that they all either require a Name to be passed as parameter or simply do not work.</p>
  
  <p>I have existing code but it isn't working properly:</p>
  
  <pre><code>public static IEnumerable&lt;T&gt; FindVisualChildren&lt;T&gt;(DependencyObject depObj) where T : DependencyObject
  {
    if (depObj != null)
    {
      for (int i = 0; i &lt; VisualTreeHelper.GetChildrenCount(depObj); i++)
      {
        DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
        if (child != null &amp;&amp; child is T)
        {
          yield return (T)child;
        }
  
        foreach (T childOfChild in FindVisualChildren&lt;T&gt;(child))
        {
          yield return childOfChild;
        }
      }
    }
  }
  </code></pre>
  
  <p>For instance it will not get a <code>DataGrid</code> within a <code>TabItem</code>.</p>
  
  <p>Any suggestions?</p>