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