Esempio n. 1
0
        public static DependencyObject FindAncestorByType(DependencyObject element, Type type, bool specificTypeOnly)
        {
            if (element == null)
            {
                return(null);
            }

            if (specificTypeOnly ? (element.GetType() == type)
                : (element.GetType() == type) || (element.GetType().IsSubclassOf(type)))
            {
                return(element);
            }

            return(VisualTreeHelperEx.FindAncestorByType(VisualTreeHelper.GetParent(element), type, specificTypeOnly));
        }
        private static void TargetUiElement_Click(object sender, RoutedEventArgs e)
        {
            var targetUiElement            = (Button)sender;
            DependencyObject parentTabItem =
                VisualTreeHelperEx.FindAncestorByType(targetUiElement, typeof(TabItem), true);

            if (parentTabItem != null)
            {
                var tabItem = parentTabItem as TabItem;
                DependencyObject parentTabControl = VisualTreeHelperEx.FindAncestorByType(tabItem, typeof(TabControl), true);
                if (parentTabControl is TabControl tabControl)
                {
                    tabControl.Items.Remove(tabItem);
                }
            }
        }
Esempio n. 3
0
        public static T FindAncestorByType <T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj == null)
            {
                return(default(T));
            }
            if (depObj is T)
            {
                return((T)depObj);
            }

            T parent = default(T);

            parent = VisualTreeHelperEx.FindAncestorByType <T>(VisualTreeHelper.GetParent(depObj));

            return(parent);
        }