日期:2014-05-17 浏览次数:20918 次
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; }
        }
    }
}
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;
        }
    }
}
 class Class1 : INotifyPropertyChanged
    {
        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;
                NotifyPropertyChanged("B");
            }
        }
        #region INotifyPropertyChanged 成员
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string ProName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(ProName));
            }
        }
        #endregion
    }
------解决方案--------------------
没有 INotifyProperty