Winform:文本框的双向数据绑定 [英] Winform: Two way data binding for textbox

查看:688
本文介绍了Winform:文本框的双向数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public partial class Form1 : Form, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public Form1()
        {
            InitializeComponent();
            textBox1.DataBindings.Clear();
            textBox1.DataBindings.Add("Text", this, "Title", true, DataSourceUpdateMode.OnPropertyChanged);
        }

        string _title ="";
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                if (Title == _title)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Title"));
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Title = "hello";
        }
    }

告诉我这条线的含义 

tell me the meaning of this line 

textBox1.DataBindings.Add("Text", this, "Title", true, DataSourceUpdateMode.OnPropertyChanged);

https:// social .msdn.microsoft.com /论坛/ SQLSERVER / EN-US / 072524f6-1bdf-4ed4-AC30-c7c06442ba46 /如何做 - 实施 - 双向-绑定式窗口形式-C?论坛= winformsdatacontrols

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/072524f6-1bdf-4ed4-ac30-c7c06442ba46/how-to-implement-two-way-databinding-in-windows-form-c?forum=winformsdatacontrols

推荐答案

这意味着textBox1控件将此对象的Title属性带入Text属性。 DataSourceUpdateMode.OnPropertyChanged表示更改Title属性时更改textBox1.Text值。这是因为Title属性是公共的,可以通过
另一个来源进行更改。因此,当值更改时,textBox1会更新。

第四个参数 - true表示允许格式化。它意味着如何通过未给出的格式字符串格式化值。因此,此参数可以设置为false而不会产生任何影响。


It means textBox1 control takes Title property of this object into Text property. DataSourceUpdateMode.OnPropertyChanged means that textBox1.Text value is changed when Title property is changed. It is because Title property is public and can be changed by another source. So textBox1 is updated when value is changed.
Fourth parameter - true is allow formatting. It means how to format value by format string which is not given. So this parameter can be set to false without some effect.


这篇关于Winform:文本框的双向数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆