C# INotifyPropertyChanged用法,监听属性值发生改变

版权声明:本文为博主原创文章,未经博主允许不得转载。http://blog.csdn.net/taoerit https://blog.csdn.net/taoerit/article/details/54290623

       有时候,我们需要监听属性值的变化,当属性值发生改变时,需要被通知,没有改变时不需要通知,这个时候可以用INotifyPropertyChanged来做,也可以自定义委托事件,也可以实现,方法多多,在这里只介绍INotifyPropertyChanged的方法,目的是当属性值发生变化时,才出发事件

     案例: 创建一个父类Person,继承INotifyPropertyChanged类,然后让Man类去继承

Person类:

 

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace Demo
{

    public delegate void ChangedHandler(object sender, object v);

    public class Person : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        public virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler hander = PropertyChanged;
            if (hander != null)
                hander(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        /// <summary>
        /// 属性变换事件
        /// </summary>
        /// <param name="sender">源</param>
        /// <param name="v">变化后的值</param>
        /// <param name="old">变化前的值</param>
        /// <param name="hander"></param>
        protected virtual void OnChanged(object sender, object v, object old, ChangedHandler hander)
        {
            if (!v.Equals(old) && hander != null)
                hander(sender, v);
        }
    }
}


Man类

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demo
{
    public class Man:Person
    {
  
        public event ChangedHandler ChangeName;

        public event ChangedHandler ChangeAge;

       string name = string.Empty;

        public virtual string Name
        {
            get { return name; }
            set
            {
                base.OnChanged(this, value, Name, ChangeName);
                name = value;
                OnPropertyChanged("Name");
            }
        }


        int age = 10;

        public virtual int Age
        {
            get { return age; }
            set
            {
                base.OnChanged(this, value, Age, ChangeAge);
                age = value;
                OnPropertyChanged("Age");

            }
        }

    }
}

 

 

Form1 界面

 

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;
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;


namespace Demo
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        Man m = new Man();

        private void Form1_Load(object sender, EventArgs e)
        {
         
            m.ChangeName += new ChangedHandler(m_ChangeName);
            m.ChangeAge += new ChangedHandler(m_ChangeAge);

        }

        void m_ChangeAge(object sender, object v)
        {
            SetText("m.Age " + v.ToString());
        }

        void m_ChangeName(object sender, object v)
        {
            SetText("m.Name " + v.ToString());
        }

        private void btnName_Click(object sender, System.EventArgs e)
        {
            m.Name = textBox2.Text;
        }

        public void SetText(string txt)
        {
            textBox1.Text += txt+ "\r\n";
        }

        private void btnAge_Click(object sender, System.EventArgs e)
        {
            m.Age = int.Parse(textBox3.Text);
        }

    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

展开阅读全文

没有更多推荐了,返回首页