(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

Tip: How to implement INotifyPropertyChanged interface?

(0 votes)
Denislav Savkov
>
Denislav Savkov
Joined Feb 11, 2008
Articles:   14
Comments:   6
More Articles
9 comments   /   posted on Aug 29, 2008
Categories:   Data Binding , General

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!


Subscribe

Comments

  • -_-

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

  • iiordanov

    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.

  • -_-

    RE: Tip: How to implement INotifyPropertyChanged interface?


    posted by Gabe on Jun 25, 2010 00:45
    This was super helpful, I thank you very much.
  • vit100

    RE: Tip: How to implement INotifyPropertyChanged interface?


    posted by vit100 on Sep 07, 2010 03:42

    I usualy have BaseModel class which implements INotifyPropertyChanged interface and has only one method:

    void RaisePropertyChangeEvent(string propertyName){
      
    if (PropertyChanged != null)
                {            
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
               
    }

    It gives you saving in typing time as you don't need repeat code and also you use intellisense to help you.

    Next step is to modify propfull snippet to include RaisePropertyChangeEvent() call. By this you decrease time to type property with back up field dramatically.

  • -_-

    RE: Tip: How to implement INotifyPropertyChanged interface?


    posted by arun on May 16, 2011 08:29
    Thanks a lot guys this article is really to beginners like me............:)

Add Comment

Login to comment:
  *      *       
Login with Facebook