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文库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值