public static T GetVisualParentByType <T>(UIElement child) where T : UIElement { var parent = VisualTreeHelper.GetParent(child); if (parent != null && parent is UIElement) { if (parent is T) { return(parent as T); } return(VisualTreeHelpers.GetVisualParentByType <T>(parent as UIElement)); } return(null); }
public static T GetVisualParentByName <T>(UIElement child, string name) where T : UIElement { var parent = VisualTreeHelper.GetParent(child); if (parent != null && parent is UIElement) { if (parent is FrameworkElement) { FrameworkElement element = parent as FrameworkElement; if (!string.IsNullOrEmpty(element.Name) && element.Name.Equals(name)) { return(element as T); } } return(VisualTreeHelpers.GetVisualParentByName <T>(parent as UIElement, name)); } return(null); }