【问题标题】:How to get all bindingsources on a form如何获取表单上的所有绑定源
【发布时间】:2016-07-12 08:18:50
【问题描述】:

我有一个带有BindingSource 的基本表单。我有一个继承自基本表单的第二个表单,并且这个第二个表单有另外 5 个绑定源。

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

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

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

但我只得到了基本表单的绑定源。然后我尝试了:

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

这也返回相同的绑定源。

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

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

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

【问题讨论】:

    标签: c# winforms bindingsource


    【解决方案1】:

    使用来自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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 2014-07-23
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 2023-03-02
      • 2015-03-26
      相关资源
      最近更新 更多