提问者:小点点

C#winform textbox databind到属性


我有一个带有一些文本框的表单,下面是我的代码:

public partial class Test : Form
{
    private readonly ICustomerBl _customerBl;
    private readonly BindingSource _bindingSource1;

    public Test(ICustomerBl customerBl)
    {
        _bindingSource1 = new BindingSource();

        _customerBl = customerBl;

        InitializeComponent();
    }

    public Customer Customer { get; set; }

    private void Test_Load(object sender, EventArgs e)
    {
        // Method 1
        textBox2.DataBindings.Add(new Binding("Text", Customer, "Name"));

        // Method 2
        textBox3.DataBindings.Add("Text", _bindingSource1, "Email", true, DataSourceUpdateMode.OnPropertyChanged);
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (_customerBl.Get(textBox1.Text) != null)
        {
            Customer = _customerBl.Get(textBox1.Text).First();
            _bindingSource1.Add(Customer);
        }
    }

Customer.cs

public class Customer : Entity
{
    public virtual string Id { get; set; }
    public virtual string Title { get; set; }
    public virtual string Name { get; set; }
    public virtual string Address1 { get; set; }
    public virtual string Address2 { get; set; }
    public virtual string Address3 { get; set; }
    public virtual string Phone { get; set; }
    public virtual string Email { get; set; }
    public virtual string JoinDate { get; set; }
    public virtual string Status { get; set; }
    public virtual string ContactPerson { get; set; }
    public virtual string TaxId { get; set; }
    public virtual double CreditLimit { get; set; }
    protected internal virtual IList<RentalAgreement> RentalAgreements { get; protected set; }
    protected internal virtual IList<RentalWithdrawal> RentalWithdrawals { get; protected set; }

    public virtual RentalAgreement GetActiveRental()
    {
        return RentalAgreements.FirstOrDefault(rental => rental.Status == "ACTIVE");
    }

    public virtual bool HasRentalAgreement()
    {
        return RentalAgreements.Count > 0;
    }

    public override string ToString()
    {
        return Name;
    }

我试着用了上面显示的两种方法,但是没有一种有效。

这里需要一些输入。

谢啦!

更新:

抱歉,我不清楚,我的问题是如何正确地实现textbox和属性之间的数据绑定,在这种情况下,属性有一种类型的客户。并且我希望数据绑定在我更新表单的Customer属性时用Customer.name更新textbox2。

谢啦!


共1个答案

匿名用户

您的Customer类应该实现IBindableComponent,如下所示:

public class Customer : Entity, IBindableComponent {
     //Your code
     //Members of IBindableComponent
     ISite iSite;
     ControlBindingsCollection dataBindings;
     BindingContext bindingContext = new BindingContext();
     public Customer(){
         dataBindings = new ControlBindingsCollection(this);
     }
     public event EventHandler Disposed;
     public void Dispose(){
         //your code for disposing
     }
     public BindingContext BindingContext {
        get { return bindingContext;}
        set {bindingContext = value;}
     }
     public ControlBindingsCollection DataBindings {
        get { return dataBindings;}
     }
     public ISite Site {
        get { return iSite;}
        set {iSite = value;}
     }
}

应将IBindableComponent的所有成员添加到Customer类中,您可能需要搜索更多有关如何实现IBindableComponent接口的信息。