实现 INotifyPropertyChanged
public class Person : INotifyPropertyChanged
{
private string name;
public string Name
{
set
{
name = value;
this.SendChangeInfo("Name");
}
get
{
return name;
}
}
private string age;
public string Age
{
set
{
age = value;
this.SendChangeInfo("Age");
}
get
{
return age;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void SendChangeInfo(string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
控件中绑定数据
textBox1.DataBindings.Add("Text", Person, "Name");
textBox2.DataBindings.Add("Text", Person, "Age");