예제 #1
0
        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);
        }
예제 #2
0
        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);
        }