【发布时间】:2016-11-09 16:13:20
【问题描述】:
在我的余额标签最初绑定到一个数字后,再次更改数据源不会再次更新值。
我想在数据库对象更改后自动更新 Windows 窗体标签,并将其重新拉入constructorData.BankAccount。
public class ConstructorData
{
public Client Client { get; set; }
public BankAccount BankAccount { get; set; }
}
private void frmTransaction_Load(object sender, EventArgs e)
{
// Pretend we populated constructor data already
// This line of code is working
bankAccountBindingSource.DataSource = constructorData.BankAccount;
}
private void lnkProcess_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
constructorData.BankAccount = db.BankAccounts.Where(x => x.BankAccountId == constructorData.BankAccount.BankAccountId).SingleOrDefault();
// What do I do here
// Doesn't work
bankAccountBindingSource.EndEdit();
bankAccountBindingSource.ResetBindings(false);
}
自动生成代码:
//
// lblAccountBalance
//
this.lblAccountBalance.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lblAccountBalance.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bankAccountBindingSource, "Balance", true));
this.lblAccountBalance.Location = new System.Drawing.Point(482, 71);
this.lblAccountBalance.Name = "lblAccountBalance";
this.lblAccountBalance.Size = new System.Drawing.Size(196, 23);
this.lblAccountBalance.TabIndex = 7;
this.lblAccountBalance.Text = "label1";
【问题讨论】:
-
不确定 Label 在代码中的位置,但您的 ConstructorData 类应该实现 INotifyDataChanging 接口。
-
@LarsTech 我从 Visual Studio 的数据源树中拖动标签,因此它自动创建了一个绑定源并将标签绑定到它。标签中的哪些代码会有所帮助?
-
即使您使用这些设置在
ConstructorData中实现INotifyPropertyChanged,您也无法使其正常工作。您应该使用 Ivan 的回答或我在回答中所说的更改设置。