C# 使用数据绑定更新标签

C# 使用数据绑定更新标签,c#,winforms,label,C#,Winforms,Label,在我的余额标签最初绑定到一个数字后,再次更改数据源不会再次更新该值 我想在数据库对象更改后自动更新Windows窗体标签,并将其重新拉入构造函数data.BankAccount public class ConstructorData { public Client Client { get; set; } public BankAccount BankAccount { get; set; } } private void frmTransaction_Load(object

在我的余额标签最初绑定到一个数字后,再次更改数据源不会再次更新该值

我想在数据库对象更改后自动更新Windows窗体标签,并将其重新拉入
构造函数data.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";
从这里开始(在表单加载中):

直接绑定到
BankAccount
实例,即使在
ConstructorData
类中实现
INotifyPropertyChanged
(如注释中所建议的)也不会有帮助

使用该设计,无论何时,只要将新的
BankAccount
实例分配给
ConstructorData.BankAccount
属性(如所示代码),您还需要将其设置为所使用的
BindingSource
DataSource

constructorData.BankAccount = db.BankAccounts.Where(x => x.BankAccountId == constructorData.BankAccount.BankAccountId).SingleOrDefault();
// What do I do here
bankAccountBindingSource.DataSource = constructorData.BankAccount;
从这里开始(在表单加载中):

直接绑定到
BankAccount
实例,即使在
ConstructorData
类中实现
INotifyPropertyChanged
(如注释中所建议的)也不会有帮助

使用该设计,无论何时,只要将新的
BankAccount
实例分配给
ConstructorData.BankAccount
属性(如所示代码),您还需要将其设置为所使用的
BindingSource
DataSource

constructorData.BankAccount = db.BankAccounts.Where(x => x.BankAccountId == constructorData.BankAccount.BankAccountId).SingleOrDefault();
// What do I do here
bankAccountBindingSource.DataSource = constructorData.BankAccount;

如果不实现
inotifPropertyChanged
Ivan的答案,您就完全需要

原因是您将对象以这种方式放置在绑定源的数据源中:
BindingSource.DataSource=constructorData.BankAccount
,因此它使用
BankAccount
属性中的对象作为数据源。如果更改
constructorData.BankAccount
的值,则不会更改
BindingSource
的数据源,它将包含上一个对象。例如,请查看以下代码:

var a = new MyClass("1");  // ← constructorData.BankAccount = something;
var b = a;                 // ← bindingSource.DataSource = constructorData.BankAccount.
a = new MyClass("2");      // ← constructorData.BankAccount = something else;
现在应该包含哪些内容?您是否希望b包含
MyClass(“1”)
?当然不是

有关更多信息,请参阅此帖子:

我可以使用INotifyPropertyChanged解决问题吗?

如果在
ConstructorData
中实现
INotifyPropertyChanged
,并以这种方式更改绑定,则是:

bankAccountBindingSource.DataSource = constructorData;
//...
this.lblAccountBalance.DataBindings.Add(new System.Windows.Forms.Binding("Text",
    this.bankAccountBindingSource, "BankAccount.Balance", true));

如果不实施INotifyPropertyChanged,Ivan的答案正是您所需要的

原因是您将对象以这种方式放置在绑定源的数据源中:
BindingSource.DataSource=constructorData.BankAccount
,因此它使用
BankAccount
属性中的对象作为数据源。如果更改
constructorData.BankAccount
的值,则不会更改
BindingSource
的数据源,它将包含上一个对象。例如,请查看以下代码:

var a = new MyClass("1");  // ← constructorData.BankAccount = something;
var b = a;                 // ← bindingSource.DataSource = constructorData.BankAccount.
a = new MyClass("2");      // ← constructorData.BankAccount = something else;
现在应该包含哪些内容?您是否希望b包含
MyClass(“1”)
?当然不是

有关更多信息,请参阅此帖子:

我可以使用INotifyPropertyChanged解决问题吗?

如果在
ConstructorData
中实现
INotifyPropertyChanged
,并以这种方式更改绑定,则是:

bankAccountBindingSource.DataSource = constructorData;
//...
this.lblAccountBalance.DataBindings.Add(new System.Windows.Forms.Binding("Text",
    this.bankAccountBindingSource, "BankAccount.Balance", true));

不确定标签在代码中的位置,但您的ConstructorData类应该实现InotifyDataChanged接口。@LarsTech我从visual studio的数据源树中拖动了标签,因此它自动创建了绑定源并将标签绑定到它。标签中的哪个代码会有帮助?。即使使用这些设置在
ConstructorData
中实现
INotifyPropertyChanged
,也无法使其正常工作。你应该使用Ivan的答案或我在回答中所说的更改设置。不确定标签在代码中的位置,但是您的ConstructorData类应该实现一个INotifyDataChanging接口。@我从visual studio的数据源树中拖动了标签,因此它自动创建了一个绑定源并将标签绑定到它。标签中的哪个代码会有帮助?。即使使用这些设置在
ConstructorData
中实现
INotifyPropertyChanged
,也无法使其正常工作。你应该使用Ivan的答案或者我在答案中所说的更改设置。