public static Visual GetDescendantByName(FrameworkElement element, string name)
{
if (element == null)
{
return null;
}
if (element.Name == name)
{
return element;
}
Visual descendantByName = null;
if (element != null)
{
element.ApplyTemplate();
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
{
Visual child = VisualTreeHelper.GetChild(element, i) as Visual;
descendantByName = GetDescendantByName((FrameworkElement) child, name);
if (descendantByName != null)
{
return descendantByName;
}
}
return descendantByName;
}