Find all controls in WPF Window by type

后端 未结 17 1266
春和景丽
春和景丽 2020-11-21 10:14

I\'m looking for a way to find all controls on Window by their type,

for example: find all TextBoxes, find all controls implementing specific i

相关标签:
17条回答
  • 2020-11-21 10:39

    To get a list of all childs of a specific type you can use:

    private static IEnumerable<DependencyObject> FindInVisualTreeDown(DependencyObject obj, Type type)
    {
        if (obj != null)
        {
            if (obj.GetType() == type)
            {
                yield return obj;
            }
    
            for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                foreach (var child in FindInVisualTreeDown(VisualTreeHelper.GetChild(obj, i), type))
                {
                    if (child != null)
                    {
                        yield return child;
                    }
                }
            }
        }
    
        yield break;
    }
    
    0 讨论(0)
  • 2020-11-21 10:41

    Small change to the recursion to so you can for example find the child tab control of a tab control.

        public static DependencyObject FindInVisualTreeDown(DependencyObject obj, Type type)
        {
            if (obj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
    
                    if (child.GetType() == type)
                    {
                        return child;
                    }
    
                    DependencyObject childReturn = FindInVisualTreeDown(child, type);
                    if (childReturn != null)
                    {
                        return childReturn;
                    }
                }
            }
    
            return null;
        }
    
    0 讨论(0)
  • 2020-11-21 10:41

    For this and more use cases you can add flowing extension method to your library:

     public static List<DependencyObject> FindAllChildren(this DependencyObject dpo, Predicate<DependencyObject> predicate)
        {
            var results = new List<DependencyObject>();
            if (predicate == null)
                return results;
    
    
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dpo); i++)
            {
                var child = VisualTreeHelper.GetChild(dpo, i);
                if (predicate(child))
                    results.Add(child);
    
                var subChildren = child.FindAllChildren(predicate);
                results.AddRange(subChildren);
            }
            return results;
        }
    

    Example for your case:

     var children = dpObject.FindAllChildren(child => child is TextBox);
    
    0 讨论(0)
  • 2020-11-21 10:43

    My version for C++/CLI

    template < class T, class U >
    bool Isinst(U u) 
    {
        return dynamic_cast< T >(u) != nullptr;
    }
    
    template <typename T>
        T FindVisualChildByType(Windows::UI::Xaml::DependencyObject^ element, Platform::String^ name)
        {
            if (Isinst<T>(element) && dynamic_cast<Windows::UI::Xaml::FrameworkElement^>(element)->Name == name)
            {
                return dynamic_cast<T>(element);
            }
            int childcount = Windows::UI::Xaml::Media::VisualTreeHelper::GetChildrenCount(element);
            for (int i = 0; i < childcount; ++i)
            {
                auto childElement = FindVisualChildByType<T>(Windows::UI::Xaml::Media::VisualTreeHelper::GetChild(element, i), name);
                if (childElement != nullptr)
                {
                    return childElement;
                }
            }
            return nullptr;
        };
    
    0 讨论(0)
  • 2020-11-21 10:46

    Here is yet another, compact version, with the generics syntax:

        public static IEnumerable<T> FindLogicalChildren<T>(DependencyObject obj) where T : DependencyObject
        {
            if (obj != null) {
                if (obj is T)
                    yield return obj as T;
    
                foreach (DependencyObject child in LogicalTreeHelper.GetChildren(obj).OfType<DependencyObject>()) 
                    foreach (T c in FindLogicalChildren<T>(child)) 
                        yield return c;
            }
        }
    
    0 讨论(0)
  • 2020-11-21 10:46

    For some reason, none of the answers posted here helped me to get all controls of given type contained in a given control in my MainWindow. I needed to find all menu items in one menu to iterate them. They were not all direct descendants of the menu, so i managed to collect only the first lilne of them using any of the code above. This extension method is my solution for the problem for anyone who will continue to read all the way down here.

    public static void FindVisualChildren<T>(this ICollection<T> children, DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                var brethren = LogicalTreeHelper.GetChildren(depObj);
                var brethrenOfType = LogicalTreeHelper.GetChildren(depObj).OfType<T>();
                foreach (var childOfType in brethrenOfType)
                {
                    children.Add(childOfType);
                }
    
                foreach (var rawChild in brethren)
                {
                    if (rawChild is DependencyObject)
                    {
                        var child = rawChild as DependencyObject;
                        FindVisualChildren<T>(children, child);
                    }
                }
            }
        }
    

    Hope it helps.

    0 讨论(0)
提交回复
热议问题