码迷,mamicode.com
首页 > 其他好文 > 详细

INotifyPropertyChanged, Interface

时间:2014-09-07 22:23:45      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   for   2014   

 Data Object(class) impliment INotifyPropertyChanged; then the Object can update BindingSource.

Implimentation

bubuko.com,布布扣
   public abstract class NotifyProperyChangedBase : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region methods

        protected bool CheckPropertyChanged<T>(string propertyName, ref T oldValue, ref T newValue)
        {
            if (oldValue == null && newValue == null)
            {
                return false;
            }

            if ((oldValue == null && newValue != null) || !oldValue.Equals((T)newValue))
            {
                oldValue = newValue;

                FirePropertyChanged(propertyName);
                
                return true;                
            }

            return false;
        }

        protected void FirePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));


            }
        }

        #endregion

    }
View Code

Use

bubuko.com,布布扣
    class NotifiablePerson : MyComponentModel.NotifyProperyChangedBase
    {
        private string _firstName;

        public string FirstName
        {
            get { return _firstName; }
            set 
            {
                if (this.CheckPropertyChanged<string>("FirstName", ref _firstName, ref value))
                {
                    this.DisplayNameChanged();
                }
            }
        }

        private string _lastName;

        public string LastName
        {
            get { return _lastName; }
            set 
            {
                if (this.CheckPropertyChanged<string>("LastName", ref _lastName, ref value))
                {
                    this.DisplayNameChanged();
                }
            }
        }

        public string DisplayName
        {
            get { return _firstName + " " + _lastName; }
        }

        private void DisplayNameChanged()
        {
            this.FirePropertyChanged("DisplayName");
        }
    }
View Code

One Way binding => textBox event -> textBox1_TextChanged(object sender, EventArgs e) update Data/Property -> Property update another control(listBox2.DataSource = nl;).

bubuko.com,布布扣
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadRegularList();

            LoadNotiList();            
        }

        private void LoadRegularList()
        {
            System.ComponentModel.BindingList<RegularPerson> rl = new System.ComponentModel.BindingList<RegularPerson>();

            RegularPerson rp = rl.AddNew();
            rp.FirstName = "John";
            rp.LastName = "Smith";

            RegularPerson rp2 = rl.AddNew();
            rp2.FirstName = "Joe";
            rp2.LastName = "Shmoe";

            listBox1.DataSource = rl;
        }

        private void LoadNotiList()
        {
            System.ComponentModel.BindingList<NotifiablePerson> nl = new System.ComponentModel.BindingList<NotifiablePerson>();
            
            NotifiablePerson np = nl.AddNew();
            np.FirstName = "Jane";
            np.LastName = "Doe";

            NotifiablePerson np2 = nl.AddNew();
            np2.FirstName = "Mary";
            np2.LastName = "Smith";

            listBox2.DataSource = nl;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                ((RegularPerson)listBox1.SelectedItem).FirstName = textBox1.Text;
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                ((RegularPerson)listBox1.SelectedItem).LastName = textBox2.Text;
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (listBox2.SelectedItem != null)
            {
                ((NotifiablePerson)listBox2.SelectedItem).FirstName = textBox3.Text;
            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (listBox2.SelectedItem != null)
            {
                ((NotifiablePerson)listBox2.SelectedItem).LastName = textBox4.Text;
            }
        }
    }
View Code

 bubuko.com,布布扣

TextBox(First Name, Last Name) => Notifiable Person => ListBox

 

INotifyPropertyChanged, Interface

标签:style   blog   http   color   os   io   ar   for   2014   

原文地址:http://www.cnblogs.com/kevinygq/p/3960804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!