C# 无法正确实现对象的属性值与控件的属性值之间的绑定
我正在尝试使用INotifyPropertyChanged实现通过客户端类对象数据绑定更新textedit控件,但无法使其工作。后面的对象(数据源)将更新,但textedit仍为空。如果我在编辑框中键入文本,数据源将被更新。你能帮忙吗?以下是我正在使用的相关代码:C# 无法正确实现对象的属性值与控件的属性值之间的绑定,c#,winforms,data-binding,C#,Winforms,Data Binding,我正在尝试使用INotifyPropertyChanged实现通过客户端类对象数据绑定更新textedit控件,但无法使其工作。后面的对象(数据源)将更新,但textedit仍为空。如果我在编辑框中键入文本,数据源将被更新。你能帮忙吗?以下是我正在使用的相关代码: public class Client : NotifyProperyChangedBase { private string _firstname; public string Firstname {
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);
公共类客户端:notifyPropertyChangedBase
{
私有字符串_firstname;
公共字符串名
{
收到
{
返回此。\u firstname;
}
设置
{
这个.CheckPropertyChanged(“Firstname”,ref\u Firstname,ref value);
}
}
}
公共客户端ClientA=新客户端();
Binding fname=新绑定(“Text”,ClientA,“Firstname”,true,DataSourceUpdateMode.OnPropertyChanged);
ultraTextEditor_firstname.DataBindings.Add(fname);
ClientA.Firstname=“testN” 我假设你的基地是按照。如果我的假设不正确,则需要提供NotifyPropertyChangedBase类的实现 您可能还想查看构造函数文档,因为它讨论了绑定试图定位的控制事件 查看该示例,您将希望尝试以下内容:
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";
System.ComponentModel.BindingList bindings=new System.ComponentModel.BindingList();
clientA=bindings.AddNew();
clientA.Firstname=“约翰”;
textEditControl.DataSource=绑定;
//这一变化大概将在控制中重新选出
clientA.Firstname=“Jane”;
更新:
在审查课程方法的文件后;我认为绑定的数据源需要实现接口才能正确参与绑定(所有MSDN示例都是实现此接口的DataSet或DataTable)。我假设您的基础是按照以下方式实现的。如果我的假设不正确,则需要提供NotifyPropertyChangedBase类的实现 您可能还想查看构造函数文档,因为它讨论了绑定试图定位的控制事件 查看该示例,您将希望尝试以下内容:
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";
System.ComponentModel.BindingList bindings=new System.ComponentModel.BindingList();
clientA=bindings.AddNew();
clientA.Firstname=“约翰”;
textEditControl.DataSource=绑定;
//这一变化大概将在控制中重新选出
clientA.Firstname=“Jane”;
更新:
在审查课程方法的文件后;我相信绑定的数据源需要实现接口才能正确参与绑定(所有MSDN示例都是实现此接口的DataSet或DataTable)。“WinForm INotifyPropertyChanged不起作用”我对此表示怀疑。“WinForm INotifyPropertyChanged不起作用”我对此非常怀疑。我认为
CheckPropertyChanged()
会引发事件,因此不需要再次引发。也就是说,如果OP正在使用本文中的类,我认为CheckPropertyChanged()
会引发事件,因此不需要再次引发。也就是说,如果OP正在使用本文中的类