Wpf find all Controlles by tag and type

前端 未结 2 1112
夕颜
夕颜 2021-01-16 15:35

I\'m trying to retrieve of all elements by type and tag name. I already found some examples:

How can I find WPF controls by name or type?

https://stackoverfl

2条回答
  •  梦谈多话
    2021-01-16 16:03

    Tried a lot of solutions recommended by the community. None really worked. Had to modify a bit. Here is what worked for me.

                List panels = new List();
            foreach (object rawChild in LogicalTreeHelper.GetChildren(dockManager))
            {
                if (rawChild is DependencyObject)
                {
                    DependencyObject child = (DependencyObject)rawChild;
                    if (child is LayoutPanel)
                    {
                        panels.Add((LayoutPanel)child);
                    }
    
                    foreach (LayoutPanel childOfChild in FindLogicalChildren(child))
                    {
                        panels.Add(childOfChild);
                    }
                }
            }
            var requiredPanel = panels.Where(t => t.Tag.ToString() == tagName).FirstOrDefault();
    

提交回复
热议问题