1

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

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

客户.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。

谢谢 !

4

1 回答 1

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接口。

于 2013-06-24T16:36:27.313 回答