public IEnumerable FindVisualChildren(DependencyObject depObj) where T : DependencyObject
{
   if (depObj != null)
   {
       for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
       {
           DependencyObject child = VisualTreeHelper.GetChild(depObj, i);

           if (child != null && child is T)
               yield return (T)child;

           foreach (T childOfChild in FindVisualChildren(child))
               yield return childOfChild;
       }
   }
}

Usage:

foreach (var rectangle in FindVisualChildren(this))
{
  if (rectangle.Name == "yourControlName")
  {
    /*   Your code here  */
  }
}

The article has reference to the source Stackoverflow (opens new window)