1 out of 3 rated this helpful - Rate this topic

INotifyPropertyChanged Interface

Notifies clients that a property value has changed.

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

The INotifyPropertyChanged type exposes the following members.

  Name Description
Public event Supported by Silverlight for Windows Phone Supported by Xbox 360 PropertyChanged Occurs when a property value changes.
Top

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed.

For example, consider a Person object with a property called FirstName. To provide generic property-change notification, the Person type implements the INotifyPropertyChanged interface and raises a PropertyChanged event when FirstName is changed.

The following code example demonstrates the how to implement the INotifyPropertyChanged interface.


//Add using statements
using System.ComponentModel;
using System.Windows.Data;


...


// Create a class that implements INotifyPropertyChanged
public class Person : INotifyPropertyChanged
{
    private string firstNameValue;
    public string FirstName{
        get { return firstNameValue; }
        set
        {
            firstNameValue=value;
            // Call NotifyPropertyChanged when the property is updated
            NotifyPropertyChanged("FirstName");
        }
    }

    // Declare the PropertyChanged event
    public event PropertyChangedEventHandler PropertyChanged;

    // NotifyPropertyChanged will raise the PropertyChanged event passing the
    // source property that is being updated.
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }  
}


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Raise event only if value changes
PropertyChanged event should be raised only if property value has changed:
if (firstNameValue != value)
{
firstNameValue=value; // Call NotifyPropertyChanged when the property is updated NotifyPropertyChanged("FirstName");
}

This will help to avoid stack overflow in some special situations when different properties depends on each other.
For Windows Phone 7
The interface is in mscorlib.Extensions and not System ;)
For Windows Phone 7
The assembly is mscorlib.Extensions and not System  !