winfrom数据绑定

实现 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");
posted @ 2021-01-28 16:17  柠檬山竹大西瓜  阅读(71)  评论(0编辑  收藏  举报