WinForm控件的数据绑定

参考学习视频链接:01_哔哩哔哩_bilibili

1、控件的数据绑定通过 DataBindings进行绑定

        label1.DataBindings.Add("Text", person, "Name");

2、控件的双向绑定 通过类的继承 INotifyPropertyChanged

在类中增加委托函数,如下类范例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp15
{
    public class Person :INotifyPropertyChanged
    {
        private string  name;

        public string  Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged();
            }
        }

        private int age;

        public event PropertyChangedEventHandler PropertyChanged;

        public int Age
        {
            get { return age; }
            set
            {
                age = value;

                if (age>45)
                {
                    //用属性来赋值,可以触发set函数,若用字段则需要增加 OnPropertyChanged("ControlColor");
                    ControlColor = Color.Red;
                    //OnPropertyChanged("ControlColor");
                }
                else
                {
                    ControlColor = Color.BlanchedAlmond;
                    //OnPropertyChanged("ControlColor");
                }
                OnPropertyChanged();
            }
        }

        private void OnPropertyChanged([CallerMemberName]string propertyName="")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private Color controlColor;

        public Color ControlColor
        {
            get { return controlColor; }
            set
            {
                controlColor = value;
                OnPropertyChanged();
            }
        }


    }
}

3、使用时先绑定即可在后面更新变量时,对控件值进行更新,范例代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp15
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Person person = new Person();
        private void button1_Click(object sender, EventArgs e)
        {
            person.Name = "wangwu";
            //person.ControlColor = Color.Beige;

            person.Age = 56;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            person.Name = "liSi";
            person.Age = 34;
            //person.ControlColor = Color.Red;


            label1.DataBindings.Add("Text", person, "Name");
            label1.DataBindings.Add("BackColor", person, "ControlColor");

            label2.DataBindings.Add("Text", person, "Age");

            timer1.Enabled = true;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            person.Age = 12;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random random = new Random();
            person.Age = random.Next(1, 100);
        }
    }
}

代码下载链接:WinFormDataBing资源-CSDN文库