C#新手之控制項資料繫結

來源:互聯網
上載者:User

我是個C#新手,最近有個程式要把TextBox的Text屬性與一個int屬性綁定,發現一個奇怪的事,在一個屬性中更改另一個屬性,TextBox控制項顯示的值就可以自動更新,但用點擊按鈕的方法就無效。代碼:

用於示範的類Class1:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace WindowsFormsApplication3{    class Class1    {        private int _a = 1;        public int A        {            get { return _a; }            set             {                if (_a != value)                {                    _b = value + 1;                    _a = value;                 }            }        }        private int _b = 2;        public int B        {            get { return _b; }            set { _b = value; }        }    }}

當A的值改變時,B變為A+1。

介面:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication3{    public partial class Form1 : Form    {        private Class1 _class1 = new Class1();        private TextBox tbA;        private TextBox tbB;        private Button button1;        public Form1()        {            tbA = new TextBox();  //與A綁定            tbB = new TextBox();  //與B綁定            button1 = new Button();                        tbA.Location = new Point(105, 39);            tbA.Name = "tbA";            tbA.Size = new Size(121, 21);                        tbB.Location = new Point(106, 118);            tbB.Name = "tbB";            tbB.Size = new Size(119, 21);                        button1.Location = new Point(148, 67);            button1.Name = "button1";            button1.Size = new Size(23, 45);            button1.TabIndex = 2;            button1.Text = "B加1";            button1.Click += new System.EventHandler(button1_Click);            this.Controls.Add(this.button1);            this.Controls.Add(this.tbB);            this.Controls.Add(this.tbA);            tbA.DataBindings.Add("Text", _class1, "A");            tbB.DataBindings.Add("Text", _class1, "B");        }        private void button1_Click(object sender, EventArgs e)        {            _class1.B += 1;        }    }}

在tbA中改變數值後按tab,tbB中的數值會自動更新。但點button1後B的值實際上是改變的,但控制項中不會更新,除非用INotifyPropertyChanged。

public int B        {            get { return _b; }            set            {                _b = value;                NotifyPropertyChanged("B");            }        }        #region INotifyPropertyChanged 成員        public event PropertyChangedEventHandler PropertyChanged;        public void NotifyPropertyChanged(string ProName)        {            if (PropertyChanged != null)            {                PropertyChanged(this, new PropertyChangedEventArgs(ProName));            }        }        #endregion

具體原因就是因為DataBindings方法綁定是有聯動效果的,你要手動賦值就不行
tbA.Text=_class1.A;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.