找到使用Enumerable.OfType&LT特定类型的所有子控件;)(或LINQ; T&GT [英] Find all child controls of specific type using Enumerable.OfType<T>() or LINQ

查看:153
本文介绍了找到使用Enumerable.OfType&LT特定类型的所有子控件;)(或LINQ; T&GT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存在 MyControl1.Controls.OfType<单选>()只搜索直通初步收集和不进入到孩子

Existed MyControl1.Controls.OfType<RadioButton>() searches only thru initial collection and do not enters to children.

是否有可能使用 Enumerable.OfType&LT查找特定类型的所有子控件; T&GT;() LINQ 无需编写自己的递归方法?像<一个href="http://bloggingabout.net/blogs/rick/archive/2008/04/18/selecting-all-the-controls-of-a-specific-type-the-built-in-linq-way.aspx">this.

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&LT特定类型的所有子控件;)(或LINQ; T&GT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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