我有一个基本表单,上面有一个 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/