简体   繁体   中英

c# winform textbox databind to property

I have a form with some textboxes, below is my code :

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

I tried to use 2 methods as above shows, but none works.

Needing some input here.

Thanks !

Update :

Sorry for not being clear, my question is how to correctly achieve databinding between a textbox and a property, in this case the property has a type of Customer. And I expect the databinding to update the textbox2 with Customer.Name when I updated the form's Customer property.

Thanks !

Your Customer class should implement IBindableComponent like this:

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

All the members of IBindableComponent should be added to your Customer class, you may want to search more on how to implement IBindableComponent interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM