使用 Enumerable.OfType<T>() 或 LINQ 查找特定类型的所有子控件 [英] Find all child controls of specific type using Enumerable.OfType<T>() or LINQ
本文介绍了使用 Enumerable.OfType<T>() 或 LINQ 查找特定类型的所有子控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现有的MyControl1.Controls.OfType
仅通过初始集合进行搜索,并且不进入子项.
Existed MyControl1.Controls.OfType<RadioButton>()
searches only thru initial collection and do not enters to children.
是否可以在不编写自己的递归方法的情况下使用 Enumerable.OfType
或 LINQ
找到特定类型的所有子控件?喜欢 这个.
Is it possible to find all child controls of specific type using Enumerable.OfType<T>()
or LINQ
without writing own recursive method? Like this.
推荐答案
我使用扩展方法来扁平化控件层次结构,然后应用过滤器,因此使用自己的递归方法.
I use an extension method to flatten control hierarchy and then apply filters, so that's using own recursive method.
方法是这样的
public static IEnumerable<Control> FlattenChildren(this Control control)
{
var children = control.Controls.Cast<Control>();
return children.SelectMany(c => FlattenChildren(c)).Concat(children);
}
这篇关于使用 Enumerable.OfType<T>() 或 LINQ 查找特定类型的所有子控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文