【问题标题】:Updating a Label with DataBinding使用 DataBinding 更新标签
【发布时间】: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 的回答或我在回答中所说的更改设置。

标签: c# winforms label


【解决方案1】:

从这里开始(在表单加载中):

bankAccountBindingSource.DataSource = constructorData.BankAccount;

您直接绑定到BankAccount 实例,即使在ConstructorData 类中实现INotifyPropertyChanged(如cmets 中所建议的那样)也无济于事。

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

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

【讨论】:

    【解决方案2】:

    没有实现INotifyPropertyChanged Ivan 的答案正是您所需要的。

    原因是因为你把一个对象放在绑定源的DataSource中是这样的: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?你期望 b 包含MyClass("1") 吗?肯定没有。

    有关更多信息,请查看此帖子:

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-03-24
      • 2014-12-04
      • 2023-03-12
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多