MVVM Design Pattern Basics--inotifypropertychanged Interface

Source: Internet
Author: User

Data binding is a very enjoyable technology on the. NET platform. Using data binding can reduce code and simplify control logic.
In general, you can bind a property of an object to a visual control, and the display data on the control changes as the property value changes. To achieve this, you only need to implement the INotifyPropertyChanged interface for your custom objects. The PropertyChanged event is defined in this interface, and we only need to trigger the event when the value of the property changes.

The INotifyPropertyChanged interface is a very important interface in wpf/silverlight development, which forms the basis of ViewModel.

INotifyPropertyChanged Interface:
Notifies the client that a property value has changed

Namespaces: System.ComponentModel
Assembly: System (in System.dll)
Syntax: public interface INotifyPropertyChanged

INotifyPropertyChanged Members:

Inotifypropertychanged.propertychanged Events
Occurs when a property is changed

Syntax: Event PropertyChangedEventHandler propertychanged

The INotifyPropertyChanged interface is used to send a notification that a property value has changed for the client (typically the client performing the binding).

To issue a change notification when a client is bound to a data source, the binding type should have any of the following features:
* Implement INotifyPropertyChanged Interface (preferred)
*MVVM Design Pattern Basics –inotifypropertychanged Interface

The following code example shows how to implement the PropertyChanged event for the INotifyPropertyChanged interface.

//This was a simple customer class that  //implements the Ipropertychange interface.   Public classdemocustomer:inotifypropertychanged {//These fields, the values for the public properties.      PrivateGuid idvalue = Guid.NewGuid ();Private stringCustomernamevalue = String.Empty;Private stringPhonenumbervalue = String.Empty; Public EventPropertyChangedEventHandler propertychanged;Private void notifypropertychanged(String info) {if(PropertyChanged! =NULL) {propertychanged ( This,NewPropertyChangedEventArgs (info)); }      }//The constructor is private to enforce the factory pattern.      Private Democustomer() {Customernamevalue ="Customer"; Phonenumbervalue ="(555) 555-5555"; }//This was the public factory method.       Public StaticDemocustomerCreatenewcustomer()      {return NewDemocustomer (); }//This property represents an ID, suitable      //For use as a primary key in a database.       PublicGuid ID {Get{return  This. idvalue; }      } Public stringCustomerName {Get{return  This. Customernamevalue; }Set{if(value!= This. customernamevalue) { This. Customernamevalue =value; Notifypropertychanged ("CustomerName"); }          }      } Public stringPhoneNumber {Get{return  This. Phonenumbervalue; }Set{if(value!= This. phonenumbervalue) { This. Phonenumbervalue =value; Notifypropertychanged ("PhoneNumber"); }          }      }  }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MVVM Design Pattern Basics--inotifypropertychanged Interface

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.