INotifyPropertyChanged的作用

Posted 大江东去奔流到海

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了INotifyPropertyChanged的作用相关的知识,希望对你有一定的参考价值。

最近学习数据驱动UI,了解到INotifyPropertyChanged这个接口的用法,看了很多网上的文章,自己作了一个总结。

INotifyPropertyChanged这个接口其实非常简单,只有一个PropertyChanged事件,如果类继承了这个接口,就必须实现接口。用VS的提示,就是补充了一句话:

public event PropertyChangedEventHandler PropertyChanged;

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

namespace Demo001
{
    class Student:INotifyPropertyChanged
    {
        private string name;
        private int age;
        public string Name
        {
            get { return name; }
            set
            {
                if (this.name == value) { return; }
                this.name = value;
                Notify("Name");
            }
        }
        public int Age
        {
            get { return age; }
            set
            {
                if (this.age == value) { return; }
                this.age = value;
                Notify("Age");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

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

        }

    }
}

剩下的就是对事件PropertyChanged的操作,于是我想可不可以直接定义这个事件而不继承接口INotifyPropertyChanged,结果发现也是可以的。

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

namespace Demo001
{
    class Student
    {
        private string name;
        private int age;
        public string Name
        {
            get { return name; }
            set
            {
                if (this.name == value) { return; }
                this.name = value;
                Notify("Name");
            }
        }
        public int Age
        {
            get { return age; }
            set
            {
                if (this.age == value) { return; }
                this.age = value;
                Notify("Age");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

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

        }

    }
}

只不过这时候的PropertyChanged是自定义的事件了,我们可以随意改变这个名字,比如pChanged,但是继承INotifyPropertyChanged接口后,只能用PropertyChanged这个名字。

在Form中注册事件,附代码:

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 Demo001
{
    public partial class Form1 : Form
    {
        Student stu = new Student();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            stu.PropertyChanged += changed;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            stu.Name = textBox1.Text;
            stu.Age = int.Parse(textBox2.Text);
        }

        public void changed(object sender, PropertyChangedEventArgs e)
        {
            switch(e.PropertyName)
            {
                case "Name":
                    Console.WriteLine("Name Changed");
                    break;
                case "Age":
                    Console.WriteLine("Age Changed");
                    break;
            }
        }

    }
}
namespace Demo001
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(59, 33);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 38);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(29, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "姓名";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(12, 80);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(29, 12);
            this.label2.TabIndex = 3;
            this.label2.Text = "年龄";
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(59, 75);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 21);
            this.textBox2.TabIndex = 2;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(59, 129);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 4;
            this.button1.Text = "设定";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(195, 182);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.Button button1;
    }
}

 

备忘:委托是特殊的类,事件是一种委托,所以事件注册,应该是“附加”方法,而不是“等于”方法。

委托将参数传给相应的方法,一个作用是(子窗体)传递参数,另一个作用是(主窗体)调用方法。

委托传递参数,可以用于窗体传值,主窗口利用子窗体构造函数传值给子窗体,子窗体将值传给委托(=子窗体传值给主窗体的方法,从而传值给主窗体)。

委托调用方法,主窗体注册方法,子窗体定义委托(事件),在子窗体给委托传值的时候触发主窗体调用方法,从而改变主窗体的一些UI变化。

 

以上是关于INotifyPropertyChanged的作用的主要内容,如果未能解决你的问题,请参考以下文章

使用属性... INotifyPropertyChanged

INotifyPropertyChanged WPF

INotifyPropertyChanged 订阅自我

INotifyPropertyChanged WPF

为嵌套(子)对象订阅 INotifyPropertyChanged

INotifyPropertyChanged 和 DependencyProperty 有啥关系?