在 UserControl 中使用 BindingSource [英] Using a BindingSource in a UserControl

查看:25
本文介绍了在 UserControl 中使用 BindingSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个字段的 UserControl,我希望将这些字段绑定到 BindingSource.我还希望 UserControl 公开一些 BindingSource 属性,以便它可以放在 Form 上并绑定到窗体上的 BindingSource.是否有捷径可寻?我意识到我可以在其 BindSource 设置器中重新绑定 UserControl 的所有控件.但这似乎是错误的.是否有一些 BindingSource 代理可以让我将用户控件中的 BindingSource 链接到表单中的 BindingSource?

I have a UserControl with multiple fields that I would like to have bound to a BindingSource. I would also like the UserControl to expose some BindingSource property so that it can be dropped on a Form and be bound to the BindingSource on the form. Is there an easy way to do this? I realize that I can rebind all of the controls of the UserControl in its BindSource setter. But this seems wrong. Is there some BindingSource Proxy that will let me link the BindingSource in the user control to the BindingSource in the form?

推荐答案

根据你的问题,我很难理解你的意图.因此,我将尽力为您提供有关此事的有趣信息.

As per your question, I can hardly get what you intend to do. Thus I will try my best to provide you with, I hope, interesting information on that matter.

首先,让我们考虑客户管理软件项目中的以下 UserControl.

First, let's consider the following UserControl in a Customer management software project.

public partial class CustomerManagementUserControl : UserControl {
    public CustomerManagementUserControl() {
        InitializeComponent();
        _customerBindingSource = new BindingSource();
    }
    public IList<ICustomer> DataSource {
        set {
            _customerBindingSource.DataSource = value;
        }
    }
    private BindingSource _customerBindingSource;
}

其次,让我们考虑以下表单,它应该是您的客户管理表单.

Second, let's consider the following Form which should be your Customer management form.

public partial class CustomerManagementForm : Form {
    public CustomerManagementForm() {
        InitializeComponent();
        _customerUserControl = new CustomerManagementUserControl();
        _customerUserControl.Name = @"customerUserControl";
    }
    private void CustomerManagementForm_Load(object sender, EventArgs e) {
        // CustomersFacade is simply a static class providing customer management features and requirements.
        // Indeed, the GetCustomers() method shall return an IList<ICustomer>.
        // The IList type and typed IList<T> are both intended to be bindable as a DataSource for DataBinding.
        _customerUserControl.DataSource = CustomersFacade.GetCustomers();
        this.Controls.Add(_customerUserControl);
    }
    private CustomerManagementUserControl _customerUserControl;
}

如果您希望在属性"窗口中使用 CustomerManagementUserControl.DataSource 属性,请考虑在您的属性定义之上添加以下内容.

If you're expecting to use CustomerManagementUserControl.DataSource property from within the Property window, please consider adding the following on top of your property definition.

[System.ComponentModel.DesignTimeVisible(true), System.ComponentModel.DesignerCategory("CustomerUserControl"), System.ComponentModel.Description("Sets the CustomerUserControl DataSource property")]

这是我猜您可能想做的一种方式.另一方面,如果您希望通过将不同类型的对象设置为 UserControl.BindingSource.DataSource 属性来获得尽可能抽象的内容,那么您将必须编写一个可以检测类型的方法对象传递,然后相应地绑定属性.如果您愿意使用 Reflection,那么您可以采用的一种不错的方法是使用 Reflection.您可以想象以任何可能的方式使用这样的多态特性,您必须自己编写一个接口,所有可绑定对象都必须实现该接口.这样,您将避免未知的属性名称,并且何时绑定您的 UserControl 的控件,您将能够将正确的属性绑定到正确的控件等等.

This is one way of doing what I guess you might want to do. On the other hand, if what you wish to do is to get the as most abstract as possible by setting a different type of object as your UserControl.BindingSource.DataSource property, then you will have to write a method which could detect the type of the object passed, then binding the properties accordingly. A nice way you could go, perhaps, is by Reflection, if you're comfortable working with it. In any possible way you may imagine working with such polymorphism features, you will have to write yourself an interface that all of your bindable objects will have to implement. This way, you will avoid unknown property names, and when will come the time to bind your UserControl's controls, you will be able to bind the correct property to the correct control and so forth.

让我们尝试以下操作:

public interface IEntity {
    double Id { get; set; }
    string Number { get; set; }
    string Firstname { get; set; }
    string Surname { get; set; }
    long PhoneNumber { get; set; }
}
public interface ICustomer : IEntity {
}
public interface ISupplier : IEntity {
    string Term { get; set; }
}
public sealed Customer : ICustomer {
    public Customer() {
    }
    public double Id { get; set; }
    public string Number { get; set; }
    public string Firstname { get; set; }
    public string Surname { get; set; }
    public long PhoneNumber { get; set; }    
}
public sealed Supplier : ISupplier {
    public Supplier() {
    }
    public double Id { get; set; }
    public string Number { get; set; }
    public string Firstname { get; set; }
    public string Surname { get; set; }
    public long PhoneNumber { get; set; }    
    public string Term { get; set; }
}

考虑到上面的代码,您可以使用 UserControl 的 DataSource 属性与 IEntity 绑定,因此您的属性可能是这样的.

Considering the above code, you could use the DataSource property of your UserControl to bind with an IEntity, so your property could like like this.

[System.ComponentModel.DesignTimeVisible(true), System.ComponentModel.DesignerCategory("CustomerUserControl"), System.ComponentModel.Description("Sets the CustomerUserControl DataSource property")]
public IList<IEntity> DataSource {
    set {
        _customerBindingSource.DataSource = value;
    }
}

也就是说,如果您希望更进一步,您可以只公开您的 UserControl 控件的 DataBindings 属性,以便在设计时设置它们.考虑到这一点,您可能希望将 BindingSource 作为公共属性公开,以便您也可以在设计时设置它,然后从此 BindinSource 中选择您的 DataMember.

That said, if you wish to push even further, you could just expose your UserControl's controls DataBindings properties in order to set them on design-time. Considering this, you will want to expose your BindingSource as a public property either so that you may set it on design-time too, then choose your DataMember from this BindinSource.

我希望这对您有所帮助,或者至少为您提供一些曲目以供进一步搜索.

I hope this helps you both a little or at least, give you some tracks for further searchings.

这篇关于在 UserControl 中使用 BindingSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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