zoukankan      html  css  js  c++  java
  • WinForm 双向数据绑定

    程序目标: 控件的属性值与对象的属性值双向绑定使窗口控件的属性值与对象的属性值保持一致。对窗口控件属性值更改后立即更新对象的属性值,对对象的属性值更改后立即更新窗口控件的属性值。

    程序完整代码包:https://pan.baidu.com/s/1JPX0BJDNiEoczYE9xXL1ow

    主要代码:

    定义控件属性要绑定对象的类:Person

    using System.ComponentModel;
    
    namespace TempTest
    {
        /// <summary>
        /// 要实现双向绑定需要继承System.ComponentModel.INotifyPropertyChange接口。若不继承此接口则只能单向绑定,对对象属性值的更改不会通知控件更新。
        /// </summary>
        class Person : INotifyPropertyChanged
        {
            #region 属性
            public string Name { get => mName; set { mName = value; SendChangeInfo("Name"); } }
            public string Address { get => mAddress; set { mAddress = value;SendChangeInfo("Address"); } }
            public int Age { get => mAge; set { mAge = value; SendChangeInfo("Age"); } }
            #endregion
            
    
    
            private string mName;
            private string mAddress;
            private int mAge;
    
            /// <summary>
            /// 属性改变后需要调用的方法,触发PropertyChanged事件。
            /// </summary>
            /// <param name="propertyName">属性名</param>
            private void SendChangeInfo(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
            /// <summary>
            /// 实现的接口。
            /// </summary>
            public event PropertyChangedEventHandler PropertyChanged;
        }
    }

    Form:

    using System;
    using System.Diagnostics;
    using System.Windows.Forms;
    namespace TempTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private Person User;
            private void Form1_Load(object sender, EventArgs e)
            {
                //创建对象初始化属性
                User = new Person()
                {
                    Name = "ABC",
                    Address = "中国 湖南",
                    Age = 30
                };
    
                //绑定数据
                textBoxName.DataBindings.Add("Text", User, "Name");
                textBoxAddress.DataBindings.Add("Text", User, "Address");
                textBoxAge.DataBindings.Add("Text", User, "Age");
            }
    
            /// <summary>
            /// 通过另外3个textBox改变对象属性值
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void buttonChangeValue_Click(object sender, EventArgs e)
            {
                User.Name = textBoxName2.Text;
                User.Address = textBoxAddress2.Text;
                int age;
                int.TryParse(textBoxAge2.Text, out age);
                User.Age = age;
                // textBoxName,textBoxAddress,textBoxAge的Text会自动更新
            }
    
            /// <summary>
            /// Debug输出对象属性值
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void buttonShow_Click(object sender, EventArgs e)
            {
                Debug.WriteLine($"{User.Name},{User.Address},{User.Age}");
            }
        }
    }
    View Code
    致读者:本人自学编程,知识薄弱,实践经验不够,博客文章难免有错误之处,希望读者能积极指正,感激不尽。 若您有更精妙的解决方案或者对文中有疑问,欢迎留言或联系我讨论问题。
  • 相关阅读:
    jmeter脚本在非GUI模式下运行_增加请求和返回_记录
    scp从一台服务器复制文件到本台服务器
    crontab定时任务配置
    jmeter非GUI模式_单点运行
    jmeter非GUI模式_分布式运行
    Dede 查询附加表
    Dede 列表文章 自增
    dede密码忘记 的修改方法
    CSS 字体描边
    教你如何去掉点击链接时的虚线
  • 原文地址:https://www.cnblogs.com/it89/p/10269910.html
Copyright © 2011-2022 走看看