WinForms:如何查找 UserControl 中的所有 BindingSource

发布于 2024-09-04 06:27:50 字数 210 浏览 12 评论 0原文

在我们正在开发的程序中,用户数据收集在 UserControls 中,这是使用 BindingSources 绑定到业务实体的数据。

我需要以编程方式查找 UserControl 中的所有 BindingSource。

由于 BindingSource 源未添加到 UserControl 的 Controls 集合中,因此我无法在那里搜索。

这可以做到吗?

In the program we are working on, the user data is collected in UserControls which is data bound to a business entity using BindingSources.

I need to find all the BindingSources in a UserControl programatically.

Since a BindingSource source is not added to the UserControl's Controls collection, I cannot search in there.

Can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月棠 2024-09-11 06:27:50

BindingSource 是一个 Component,而不是 Control,因此您确实无法在 Controls 集合中找到它。但是,当您使用设计器添加组件时,它会创建一个名为 components、类型为 IContainer 的字段,并向其中添加组件。该字段是私有的,因此您只能从声明它的类中访问它(除非您使用反射)。

我认为实现您想要的效果的最简单方法是将 GetBindingSources 方法添加到所有使用的控件中:

public IEnumerable<BindingSource> GetBindingSources()
{
    return components.Components.OfType<BindingSource>();
}

当然,它仅适用于使用设计器创建的 BindingSources ,不适用于您动态创建的内容(除非您将它们添加到容器中)

BindingSource is a Component, not a Control, so indeed you can't find it in the Controls collection. However, when you add components with the designer, it creates a field named components of type IContainer and adds the components to it. The field is private, so you can only access it from the class in which it is declared (unless you use reflection).

I think the easiest way to achieve what you want is to add a GetBindingSources method to all your using controls :

public IEnumerable<BindingSource> GetBindingSources()
{
    return components.Components.OfType<BindingSource>();
}

Of course, it will work only for BindingSources created with the designer, not for those you create dynamically (unless you add them to the container)

划一舟意中人 2024-09-11 06:27:50

最大的问题是找到一种解决方案,使我的方法可用于所有 UserControls,并且仍然能够使用 Visual Studio 中的 WinForms 设计器。

因为我不知道在不是从 UserControl 派生的类上使用设计器的任何方法,所以我创建了一个没有任何方法的接口,IBusinessEntityEditorView,以及采用此类视图的扩展方法,使用反射来查找组件字段我在其中搜索我的 BindingSources:

public interface IBusinessEntityEditorViewBase
{
}

...

public static void EndEditOnBindingSources(this IBusinessEntityEditorViewBase view)
{
    UserControl userControl = view as UserControl;
    if (userControl == null) return;

    FieldInfo fi = userControl.GetType().GetField("components", BindingFlags.NonPublic | BindingFlags.Instance);
    if (fi != null)
    {
        object components = fi.GetValue(userControl);
        if (components != null)
        {
            IContainer container = components as IContainer;
            if (container != null)
            {
                foreach (var bindingSource in container.Components.OfType<BindingSource>())
                {
                    bindingSource.EndEdit();
                }
            }
        }
    }
}

The biggest problem was to find a solution for my method to be available for all the UserControls and still be able to use the WinForms designer from Visual Studio.

Because I don't know any way of using the designer on a class that does not derive from UserControl, I have made an interface without any methods, IBusinessEntityEditorView, and an extension method that takes such a view, uses reflection to find the components field in which I search for my BindingSources:

public interface IBusinessEntityEditorViewBase
{
}

...

public static void EndEditOnBindingSources(this IBusinessEntityEditorViewBase view)
{
    UserControl userControl = view as UserControl;
    if (userControl == null) return;

    FieldInfo fi = userControl.GetType().GetField("components", BindingFlags.NonPublic | BindingFlags.Instance);
    if (fi != null)
    {
        object components = fi.GetValue(userControl);
        if (components != null)
        {
            IContainer container = components as IContainer;
            if (container != null)
            {
                foreach (var bindingSource in container.Components.OfType<BindingSource>())
                {
                    bindingSource.EndEdit();
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文