Wpf find all Controlles by tag and type

前端 未结 2 1085
夕颜
夕颜 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<LayoutPanel> panels = new List<LayoutPanel>();
            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<LayoutPanel>(child))
                    {
                        panels.Add(childOfChild);
                    }
                }
            }
            var requiredPanel = panels.Where(t => t.Tag.ToString() == tagName).FirstOrDefault();
    
    0 讨论(0)
  • 2021-01-16 16:08

    You could use the following FindVisualChildren method:

    Find all controls in WPF Window by type

    ...and simply filter the results:

    string tagName = "tagname";
    IEnumerable<Ellipse> elements = FindVisualChildren<Ellipse>(this).Where(x => x.Tag != null && x.Tag.ToString() == tagName);
    
    0 讨论(0)
提交回复
热议问题