【问题标题】:Unable to correctly implement a binding between the property value of an object and the property value of a control无法正确实现对象的属性值和控件的属性值之间的绑定
【发布时间】:2011-08-15 03:13:17
【问题描述】:

我正在尝试通过具有 INotifyPropertyChanged 实现的客户端类对象数据绑定来更新 textedit 控件,但我无法使其工作。 (数据源)后面的对象更新,但文本编辑仍然保持空白。如果我在编辑框中输入文本,数据源就会更新。你能帮忙吗?这是我正在使用的相关代码:

public class Client : NotifyProperyChangedBase
{

    private string _firstname;
    public string Firstname
    {
        get
        {
            return this._firstname;
        }
        set
        {
            this.CheckPropertyChanged<string>("Firstname", ref _firstname, ref value);
        }
    }
}


public Client ClientA = new Client();

Binding fname = new Binding("Text", ClientA, "Firstname", true, DataSourceUpdateMode.OnPropertyChanged);

ultraTextEditor_firstname.DataBindings.Add(fname);

ClientA.Firstname = "testN";

我在这里遗漏了什么吗?在此先感谢彼得。

【问题讨论】:

  • “WinForm INotifyPropertyChanged 不起作用”我非常怀疑。

标签: c# winforms data-binding


【解决方案1】:

我假设您的基础是按照this example 的方式实现的。如果我的假设不正确,您需要提供 NotifyProperyChangedBase 类的实现。

您可能还想查看Binding(String, Object, String, Boolean, DataSourceUpdateMode) 构造函数文档,因为它讨论了绑定尝试定位的控制事件。

看看那个例子,你会想尝试这样的事情:

System.ComponentModel.BindingList<Client> bindings = new System.ComponentModel.BindingList<Client>();

Client clientA = bindings.AddNew();
clientA.Firstname = "John";

textEditControl.DataSource = bindings;

// This change presumably will be refelected in control
clientA.Firstname = "Jane";

更新: 在查看了ControlBindingsCollection 类的Add 方法的文档后;我认为Binding的数据源需要实现IListSource接口才能正常参与绑定(所有MSDN例子都是实现这个接口的DataSet或DataTable)。

【讨论】:

  • 我认为CheckPropertyChanged&lt;T&gt;() 引发了事件,因此不需要再次引发。也就是说,如果 OP 正在使用这篇文章中的类codeproject.com/KB/cs/…
猜你喜欢
  • 2016-09-17
  • 2010-11-23
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多