WPF - 获取页面上给定类型的所有控件的集合? [英] WPF - Getting a collection of all controls for a given type on a Page?

查看:297
本文介绍了WPF - 获取页面上给定类型的所有控件的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得给定页面上给定类型的所有控件的列表,但是我遇到了问题。看来有可能VisualTreeHelper只返回已加载的控件?我试图关闭虚拟化,但似乎没有帮助。任何人都可以想到另一种方式来获得所有的控制或强制的UI的负载,以便下面的方法的工作吗?

我从MSDN借了这个:

p>

  public static IEnumerable< T> FindVisualChildren< T>(DependencyObject depObj)其中T:DependencyObject 
{
if(depObj!= null)
{
for(int i = 0; i< VisualTreeHelper.GetChildrenCount (depObj); i ++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj,i);

if(child!= null&& child是T)
{
yield return(T)child; (child)中的childOfChild(T)(child))
{
yield return childOfChild;
}

foreach(T childOfChild in FindVisualChildren< T>



$ b $ / code $ / pre

解决方案

请参阅以下主题:



陶亮的回答是一个很好的解释 p>


原因是WPF设计者想要优化TabControl的性能
。假设有5个TabItems,并且每个TabItem都包含
的许多子节点。如果WPF程序必须构建和渲染所有
的孩子,那么将会非常缓慢。但是,如果TabControl只处理当前选定的TabItem中的
子元素,那么内存将会被
保存。

你可以给逻辑树一个试试。

这里是一个示例实现,看看它是否更适合你



像这样使用它。

  List< Button> buttons = GetLogicalChildCollection< Button>(yourPage); 

GetLogicalChildCollection

  public static List< T> GetLogicalChildCollection< T>(object parent)其中T:DependencyObject 
{
List< T> logicalCollection = new List< T>();
GetLogicalChildCollection(父为DependencyObject,logicalCollection);
返回logicalCollection;

private static void GetLogicalChildCollection< T>(DependencyObject parent,List< T> logicalCollection)其中T:DependencyObject
{
IEnumerable children = LogicalTreeHelper.GetChildren(parent);
foreach(对象子对象)
{
if(child是DependencyObject)
{
DependencyObject depChild = child作为DependencyObject;
if(child is T)
{
logicalCollection.Add(child as T);
}
GetLogicalChildCollection(depChild,logicalCollection);
}
}
}


I'm trying to get a list of all controls of a given type on a given Page, but I'm encountering problems. It seems that it's possible that the VisualTreeHelper only returns controls that have been loaded? I tried turning off Virtualization, but that didn't seem to help. Can anyone think of another way to get all the control or perhaps force a load of the UI so that the following method does work?

I borrowed this from MSDN:

 public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);

                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }

解决方案

See the following thread: Finding all controls of a given type across a TabControl

The answer from Tao Liang is a good explanation

The reason is that the WPF designer want to optimize the performance of TabControl. Suppose there are 5 TabItems, and each TabItem contains alot of children. If WPF program have to construct and render all the children, it will be very slow. But if TabControl only handle the children just in the current selected TabItem, much memory will be saved.

You can give the logical tree a try instead.
Here is an example implementation of this, see if it works better for you

Use it like this..

List<Button> buttons = GetLogicalChildCollection<Button>(yourPage);

GetLogicalChildCollection

public static List<T> GetLogicalChildCollection<T>(object parent) where T : DependencyObject
{
    List<T> logicalCollection = new List<T>();
    GetLogicalChildCollection(parent as DependencyObject, logicalCollection);
    return logicalCollection;
}
private static void GetLogicalChildCollection<T>(DependencyObject parent, List<T> logicalCollection) where T : DependencyObject
{
    IEnumerable children = LogicalTreeHelper.GetChildren(parent);
    foreach (object child in children)
    {
        if (child is DependencyObject)
        {
            DependencyObject depChild = child as DependencyObject;
            if (child is T)
            {
                logicalCollection.Add(child as T);
            }
            GetLogicalChildCollection(depChild, logicalCollection);
        }
    }
}

这篇关于WPF - 获取页面上给定类型的所有控件的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆