The role of INotifyPropertyChanged,

Source: Internet
Author: User

The role of INotifyPropertyChanged,

I recently learned about the data-driven UI, learned how to use the INotifyPropertyChanged interface, read many online articles, and I made a summary myself.

INotifyPropertyChanged is actually very simple. There is only one PropertyChanged event. If the class inherits this interface, the interface must be implemented. With the prompt of VS, the following sentence is added:

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));            }        }    }}

The rest is the operation on the event PropertyChanged, so I thought it would be possible to directly define this event without inheriting the interface 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));            }        }    }}

In this case, PropertyChanged is a custom event. We can change this name at will, for example, pChanged. But after inheriting the INotifyPropertyChanged interface, we can only use the PropertyChanged name.

Register an event in Form with code:

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> /// a required designer variable. /// </Summary> private System. ComponentModel. IContainer components = null; // <summary> /// clear all resources in use. /// </Summary> /// <param name = "disposing"> true if the managed resource should be released; otherwise, false. </Param> protected override void Dispose (bool disposing) {if (disposing & (components! = Null) {components. dispose ();} base. dispose (disposing );} # region Windows Form Designer generated code /// <summary> /// the designer supports the required methods-do not modify /// use the code editor to modify the content of this method. /// </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 = "name"; // 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 = "Age"; // 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. points (59,129); this. button1.Name = "button1"; this. button1.Size = new System. drawing. size (75, 23); this. button1.TabIndex = 4; this. button1.Text = "set"; this. button1.UseVisualStyleBackColor = true; this. button1.Click + = new System. eventHandler (this. button#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. extends mlayout () ;}# 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 ;}}

 

Memo: A delegate is a special class, and an event is a delegate. Therefore, the event registration should be an "additional" method, not an "equal" method.

The delegate sends the parameter to the corresponding method. One function is to pass the parameter (sub-form), and the other is to call the method (main form.

The delegate transfer parameter can be used to pass values to the form. The main window uses the subform constructor to pass values to the subform. The subform passes the value to the Delegate (= the method for passing values to the main form, to pass the value to the main form ).

The delegate call method, the primary form registration method, and the subform defines the delegate (Event). When the subform sends a value to the Delegate, the call method of the primary form is triggered, which changes some UI changes of the primary form.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.