INotifyPropertyChanged的作用

摘要:
VS提示符添加了一句话:publiceventPropertyChangedEventHandlerPropertyChanged;使用系统;使用System.Collections。通用的使用系统。组件模型;使用系统。Linq;使用系统。文本namespaceDemo001{classStudent:INotifyPropertyChanged{privatestringname;privateintage;publicstringName{get{returnname;}设置{if{return;}这name=值;通知;}}publicintAge{get{returnage;}设置{if{return;}这年龄=值;通知;}}publiceventPropertyChangedEventHandlerPropertyChanged;protectedvoidNotify{if(this.PropertyChanged!=null){PropertyChanged;}}}}剩下的是对事件PropertyChanged的操作,因此我想知道是否可以直接定义此事件而不继承接口INotifyPropertyChanged。也可以=null){PropertyChanged;}}}}但是,PropertyChanged是用户定义的事件。我们可以随意更改名称,例如pChanged。但是,在继承INotifyPropertyChanged接口之后,我们只能使用名称PropertyChanged。在表单中注册事件,代码为:usingSystem;使用System.Collections。通用的使用系统。组件模型;使用系统。数据使用系统。绘画使用系统。Linq;使用系统。文本使用System.Windows。形式;namespaceDemo001{publicpartialclassForm1:Form{Studentstu=newStudent();publicForm1(){InitializeComponent();}privatevoidForm1_加载{stu.PropertyChanged+=已更改;}privatevoidbutton1_单击{stu.Name=textBox1.Text;stu.Age=int.Parse;}publicvoidchanged{switch{case“Name”:Console.WriteLine;break;case“Age”:Console.WriteLine;break;}}}NamespaceDemo001{partialclassForm1{/////<summary˃///必需的设计器变量。////privateSystem.ComponentModel.IContainercomponents=null;///<汇总˃///清理所有使用中的资源。=null){components.Dispose();}基础处置;}#regionWindows窗体设计器生成的代码//////设计器支持的必需方法-不要修改///使用代码编辑器修改此方法的内容。

最近学习数据驱动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的作用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇python基础-6 字典相关练习题001python基础下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C#WinForm仿qq窗体拖到windows窗体边上时,自动隐藏C#WinForm

C#WinForm仿qq窗体拖到windows窗体边上时,自动隐藏C#WinForm。代码:     public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }         privat...

使用Qt实现MDI风格的主窗体

文章来源:http://hi.baidu.com/wuyunju/item/3d20164c99a276f6dc0f6c52 QT提供了MDIArea控件可以很方便的实现标准的MDI窗体,但用起来并不方便.感觉像360浏览器一样通过页签来切换子窗体的方式比较好.点击菜单项或工具栏上的按钮创建新的页签,并生成一个子窗体.点击页签上的叉号关闭页签并释放子窗体对...

怎样能使winform窗体的大小固定住,不能调整其大小

窗体FormBorderStyle属性设置为:FixedSingle,再把最大化禁用就可以了 使用.Net编写Windows程序,对于窗体控制常见项目 属性:1、让窗体在启动时在指定位置出现 form1.StartPosition Manual CenterScreen WindowsDefaultLocation (default) Window...

WinForm界面布局控件WeifenLuo.WinFormsUI.Docking"的使用 (二)

WinForm界面布局控件WeifenLuo.WinFormsUI.Docking"的使用 (二) 编写人:CC阿爸   2015-1-29   今天我想与大家继续一起分享这一伟大的控件。有兴趣的同学,可以一同探讨与学习一下,否则就略过吧。 1.    DockPanel的一点点改进: 在浏览网上的一些技术文章发现,的确有些地方还是可以进一步改进,如当双击...

winform 父窗体与子窗体数据传递

简单实例截图: 父窗体代码如下: public partial class Form1 : Form{Child2 child2 = new Child2();public Form1(){InitializeComponent();child2.receive += new Child2.receiveData(testReceive);} privat...

如何弹出一个模式窗口来显示进度条 .

最近看了好多人问这方面的问题,以前我也写过一篇blog,里面说了如何在子线程中控制进度条。但目前大多数环境,需要弹出模式窗口,来显示进度条,那么只需要在原先的基础上稍作修改即可。   首先是进度条窗体,需要在上面添加进度条,然后去掉ControlBox。除此外,还要增加一个方法,用来控制进度条的增加幅度,具体如下:     /// <summary&...