[英]c# winform textbox databind to property
我有一個帶有一些文本框的表單,下面是我的代碼:
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。
謝謝 !
您的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
接口的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.