c#winform textbox databind to property

时间:2013-06-24 15:48:40

标签: c# winforms data-binding textbox bind

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

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;
    }

我尝试使用上面显示的2种方法,但都没有效果。

需要一些输入。

谢谢!

更新:

很抱歉不清楚,我的问题是如何正确实现文本框和属性之间的数据绑定,在这种情况下,属性具有Customer类型。我希望数据绑定在我更新表单的Customer属性时用Customer.Name更新textbox2。

谢谢!

1 个答案:

答案 0 :(得分:0)

您的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界面。