📜  wpf app如何获取暴露给脚本的所有元素 - C#代码示例

📅  最后修改于: 2022-03-11 14:49:00.128000             🧑  作者: Mango

代码示例1
public static IEnumerable FindVisualChildren(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj == null)
            {
                yield return null;
            }
            else
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    var child = VisualTreeHelper.GetChild(depObj, i);

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

                    foreach (T childOfChild in FindVisualChildren(child))
                        yield return childOfChild;
                }
            }
        }
//usage:

foreach (var tb in FindVisualChildren(window))
{
    // do something with tb here
}