c# - 如何获取表单上的所有绑定(bind)源

标签 c# winforms bindingsource

我有一个基本表单,上面有一个 BindingSource。我有第二种形式继承自基本形式,第二种形式有另外 5 个绑定(bind)源。

我想获取第二种形式(即 6)中的绑定(bind)源列表。

所以,在基本形式的OnLoad中,我首先尝试:

var list = this.Controls.OfType<BindingSource>();

但我只获得了基本表单的绑定(bind)源。然后我尝试了:

var List = (from Component bs in this.components.Components
            where bs is BindingSource
            select bs);

它也返回相同的绑定(bind)源。

在基本表单的 OnLoad 中执行上述操作应该可行,因为我可以获得第二种表单的所有控件。

但是,我似乎无法获得第二种形式的绑定(bind)源。

那么列出它们的正确方法是什么?

最佳答案

使用来自 Find components on a windows form c# (not controls) 的答案,接受的答案是返回一些控件,所以我添加了对 Name 属性的检查(哪些组件在运行时没有):

private IEnumerable<Component> EnumerateComponents() {
  return from field in GetType().GetFields(
              BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
         where typeof(Component).IsAssignableFrom(field.FieldType)
         let component = (Component)field.GetValue(this)
         where component != null
         where component.GetType().GetProperty("Name") == null
         select component;
}

关于c# - 如何获取表单上的所有绑定(bind)源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205318/

相关文章:

c# - 监听 COM 端口时跨线程操作无效

c# - 使用 Graphics.FromHwnd 在屏幕上绘图和清除

c# - 在 Windows 应用程序中显示结果

vb.net - 根据 DataTable 行获取 BindingSource 位置

c# - BindingSource.查找多个列

c# - 无论构建器的 WithMethods(..) 中的规范如何,似乎都允许 GET 和 POST

c# - 使用 LINQ C# 将模型类集合转换为基于属性作为 KEY 的字典

c# - 如何在C#中加密和解密字符串

c# - C#中的定时器问题

sorting - 使用 bindingsource 在正确位置向用户排序的 wingrid 添加新行