C# winform控件和对象双向数据绑定


控件和对象双向数据绑定


实现结果:

1. 对象值 -> 控件值

2. 控件值 -> 对象值


image.png


演示代码:

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 控件与对象的双向绑定
{
    public partial class Form1 : Form
    {
        UserInfo user = new UserInfo();


        public Form1()
        {
            InitializeComponent();

            user.UserName = "勇哥";
            user.Id = 1;
            //user.PropertyChanged += User_PropertyChanged;

        }

        public void MyMethod(string paramName = nameof(MyMethod))
        {
            Console.WriteLine($"The name of the parameter is: {paramName}");
        }



        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //LabUserName.Text = user.UserName;
            //LabID.Text = user.Id.ToString();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }


        int js1 = 0;
        private void button2_Click(object sender, EventArgs e)
        {
            user.UserName = $"刘备{++js1}";
            user.Id = 1+js1;
        }

     
        MyClass mycls = new MyClass();

        private void Form1_Load(object sender, EventArgs e)
        {
            BindingSource source = new BindingSource();
            source.DataSource = mycls;

            this.trackBar1.DataBindings.Add("Value", source, "Value1", true, DataSourceUpdateMode.OnPropertyChanged);
            this.textBox1.DataBindings.Add("Text", source, "Value1", true, DataSourceUpdateMode.OnPropertyChanged);
            this.LabValue.DataBindings.Add("Text", source, "Value1", true, DataSourceUpdateMode.OnPropertyChanged);


            BindingSource source2 = new BindingSource();
            source2.DataSource = user;
            TxtUserName.DataBindings.Add("Text", source2, "UserName", true, DataSourceUpdateMode.OnPropertyChanged);
            TxtId.DataBindings.Add("Text", source2, "Id", true, DataSourceUpdateMode.OnPropertyChanged);
            LabUserName.DataBindings.Add("Text", source2, "UserName", true, DataSourceUpdateMode.OnPropertyChanged);
            LabID.DataBindings.Add("Text", source2, "Id", true, DataSourceUpdateMode.OnPropertyChanged);

        }

        public class MyClass
        {
            public int Value1 { get; set; }
        }


        public class UserInfo : INotifyPropertyChanged
        {
            private string userName;
            private int id;

            public event PropertyChangedEventHandler PropertyChanged;

            public string UserName
            {
                get
                {
                    return userName;
                }
                set
                {
                    userName = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(userName));
                }
            }

            public int Id
            {
                get
                {
                    return id;
                }
                set
                {
                    id = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(id.ToString()));
                }
            }
        }

    }
}


几点说明:

(1)对控件的修改可以实时反馈到类对象上。但当我们修改类对象属性时,控件在这时不会更改,如果想要类属性修改是可以实时反馈到控件上,我们需要实现 INotifyPropertyChanged接口。

(2) DataBindings.Add方法有6个重载。分别是:属性名、数据源、数据成员、格式化使能、数据源更新模式、数据源为空时的默认值。因此,我们可以这样写:

private void Form1_Load(object sender, EventArgs e)
{
     BindingSource source = new BindingSource();
     source.DataSource = this.trackBar1;
     this.textBox1.DataBindings.Add("Text", source, "Value");
     this.label1.DataBindings.Add("Text", source, "Value");
}

(3)数据源更新模式有三种

   1。OnValidation  验证时更新数据源

  

验证时更新数据源,在textbox控件失去焦点时,触发Onvalidation事件,继而更改TrackBar控件value属性。默认模式


  2。OnPropertyChanged 立即更新数据源

在我修改textbox控件值时,会立即修改TrackBar控件value值


3。Never 单向绑定

修改Textbox值时,TrackBar控件value属性不会更改。



--------------------- 

作者:hackpig

来源:www.skcircle.com

版权声明:本文为博主原创文章,转载请附上博文链接!



本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:
本帖最后由 勇哥,很想停止 于 2024-01-15 13:41:13 编辑

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2024年4月    »
1234567
891011121314
15161718192021
22232425262728
2930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864