Get and Iterate through Controls from a TabItem?

后端 未结 3 1256
囚心锁ツ
囚心锁ツ 2020-12-19 18:30

How to get all the Controls/UIElements which are nested in a Tabitem (from a TabControl)?

I tried everything but wasn\'t able to get them.

(Set the SelectedT

3条回答
  •  执念已碎
    2020-12-19 18:54

    for me VisualTreeHelper.GetChildrenCount always returns 0 for tab control, I had to use this method instead

     public static List ObtenerControles(DependencyObject parent)
        where T : DependencyObject
        {
            List result = new List();           
            if (parent != null)
            {
                foreach (var child in LogicalTreeHelper.GetChildren(parent))
                {                   
                    var childType = child as T;
                    if (childType != null)
                    {
                        result.Add((T)child);
                    }
    
                    foreach (var other in ObtenerControles(child as DependencyObject))
                    {
                        result.Add(other);
                    }
                }
            }
    
            return result;
        }
    

提交回复
热议问题