Recommended

Skip Navigation LinksHome / Tips / View Tip

Tip: How to implement INotifyPropertyChanged interface?

+ Add to SilverlightShow Favorites
6 comments   /   posted by Denislav Savkov on Aug 29, 2008
(0 votes)
Categories: Binding

INotifyPropertyChanged interface is used to notify that a property has been changed and thus to force the bound objects to take the new value.

C#

public class Client : INotifyPropertyChanged
{
    private string name;
    public event PropertyChangedEventHandler PropertyChanged;
 
    public string Name
    {
        get
        {
            return this.Name;
        }
        set
        {
            if ( this.name == value )
                return;
 
            this.name = value;
            this.OnPropertyChanged( new PropertyChangedEventArgs( "Name" ) );
        }
    }
 
    protected virtual void OnPropertyChanged( PropertyChangedEventArgs e )
    {
        if ( this.PropertyChanged != null )
            this.PropertyChanged( this, e );
    }
}

Note that the value is changed only if the new value is different. Thus we prevent the firing of the PropertyChanged event when the actual value is not changed.

Alternative way is to provide changed event for each property.

That's it!

Share


Comments

Comments RSS RSS
  • RE: Tip: How to implement INotifyPropertyChanged interface?  

    posted by Tapani on Oct 08, 2008 06:11

    If you define event this way:       

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    then you can remove the if-clause from OnPropertyChanged.

  • RE: Tip: How to implement INotifyPropertyChanged interface?  

    posted by Sriram on Sep 10, 2009 08:38
    Awesome stuff man. Helped me a lot. Thanks
  • RE: Tip: How to implement INotifyPropertyChanged interface?  

    posted by Huisman on Oct 28, 2009 13:43
    Is it possible to:
    define a class on my wcf-service with an INotifyPropertyChanged and then I contact the wcf-service with Silverlight and create a new instance of this class and then using a timer, I will update the information in this object using an Timer and when the information changes, the interface also changes automaticaly. Or is the INotifyPropertyChanged  propety not stirallizable?

    Thanks


  • RE: Tip: How to implement INotifyPropertyChanged interface?  

    posted by Fizz on Nov 08, 2009 00:33

    Be Safe

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        
    PropertyChangedEventHandler eventHandler = this.PropertyChanged;
        
    if (eventHandler != null)
        {
            eventHandler(
    this, e);
        }
    }

  • RE: Tip: How to implement INotifyPropertyChanged interface?  

    posted by iiordanov on Nov 08, 2009 12:07
    Nice addition Fizz, thanks :)
  • RE: Tip: How to implement INotifyPropertyChanged interface?  

    posted by DanaH on Dec 22, 2009 14:22
    Hi i have problem with using timer in my slApp, i have textbox and bind its text to property of object which is in page.datacontext

    first time i see the value of this property, but if i try to change it in the method calld when timer elapsed, it isnt in textbox.

     public class vm
        {
            string s = "nejakyString";
            public event EventHandler SChanged;
            public string S
            {
                get { return s; }
                set { s = value;
                    raiseEventPropertyChanged("S");
                }
            } public class vm
        {
            string s = "nejakyString";
            public event EventHandler SChanged;
            public string S
            {
                get { return s; }
                set { s = value;
                    raiseEventPropertyChanged("S");
                }
            }
    public event PropertyChangedEventHandler PropertyChanged;
            private void raiseEventPropertyChanged(string propertyName)
            {
              if (PropertyChanged != null)
                {           
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
               }

    Page.xaml.cs

                TimerCallback timerDelegate =
                    new TimerCallback(elapsed);

                vievModel = new vm();
                this.DataContext = vievModel;

     

       public void elapsed(Object stateInfo)
        {vievModel.S += DateTime.Now.ToString();}

    Page.xaml

    <TextBox  Name="mujTextbox" Text="{Binding Path=S}"

    thanks for helping me.

Add Comment

 
 

   
  
  
   
Please add 4 and 7 and type the answer here:

Help us make SilverlightShow even better and win a free t-shirt. Whether you'd like to suggest a change in the structure, content organization, section layout or any other aspect of SilverlightShow appearance - we'd love to hear from you! Need a material (article, tutorial, or other) on a specific topic? Let us know and SilverlightShow content authors will work to have that prepared for you. (hide this)