We help IT Professionals succeed at work.

How to use INotifyPropertyChanged?

Hi
In trying to view the value of a property of a an object and I'm not sure why this doesn't work.

1. I have a windows Form that instantiates a class called Counter and calls a member function DoCount that counts from 1 to 1000 and sets the value of a property named CurrentCount as it loops thru.
2. The class implements INotifyPropertyChange.
3. Textbox1 on the form is bound to the property CurrentCount.

Now when I run the application I should see the text in the text box change from 1 to 1000, but I dont. here is the class.

Q1. what should I do to be able to see the current value of the the property CurrentCount from the form? Q2. When I step thru this code the PropertyChanged event is always null in the FirePropertyChangedNotification method how do get that event to be fired?

public class Counter: INotifyPropertyChange
{
   public event PropertyChangedEventHandler PropertyChanged;  
   private int m_currentCount;
   public int CurrentCount
   {
      get { return m_currentCount; }
      set
      {
         m_currentCount = value;
         FirePropertyChangedNotification("CurrentCount");
      }
   }

   public void DoCount
   {
         for (int i = 0; i < 1000; i++)
                {
                    CurrentCount = i;
                }
   }
   private void FirePropertyChangedNotification(string propName)
   {
      if (PropertyChanged != null)
         PropertyChanged(this,
            new PropertyChangedEventArgs(propName));
   }  
}

Comment
Watch Question

CERTIFIED EXPERT
Commented:
private PropertyChangedEventHandler propertyChanged;
        public event PropertyChangedEventHandler PropertyChanged
        {
            add { this.propertyChanged += value;}
       
        }
You got a mistake in the name of the interface.
Shouldn't it be INotifyPropertyChanged

You should not be able to see counting from 1 to 1000, this code counts from 0 to 999.

There could be something wrong in the databinding since you don't get the PropertyChanged event set.

Could you post your databind statement?

Author

Commented:
Hi p_davis:
I think you are right however I still get PropertyChanged = null in the following code above

private void FirePropertyChangedNotification(string propName)
   {
      if (PropertyChanged != null)
         PropertyChanged(this,
            new PropertyChangedEventArgs(propName));
   }  

when does PropertyChanged  get set != null?
could you illustrate in a sample?
thanks

hi Rocky Master
yes you are right it should be INotifyPropertyChanged (sorry for the typo)
also yes it will count from 0 to 999
I haven't bound the object to anything yet I am trying to see if i can get the object to raise the event when the data is changed by it self.  
I haven't bound the object to anything yet I am trying to see if i can get the object to raise the event when the data is changed by it self.  

--> no binding --> PropertyChanged = null
To be more clear, there's no need to inform anybody when there's nobody listening.
PropertyChanged will become unequal to null when a listener gets attached

Author

Commented:
aah I see,
So this should be in my calling component? If not can you give me an example of how I would bind the property to the text box on my form? I was planning on using the Databindings property on the text box.

private PropertyChangedEventHandler propertyChanged;
        public event PropertyChangedEventHandler PropertyChanged
        {
            add { this.propertyChanged += value;}
       
        }

Author

Commented:
I see what you meant
I did get the databinding to work by adding the following code to my form's constructor

 textBoxCount.DataBindings.Add("Text", _accountUpdaterController, "CurrentCount");

thank you both for your help

Author

Commented:
I did not know how to bind to a text box once I realized the trick is to map the text box's Text property to the object's property it all fell in to place in my mind. thank you for your help
exactly like 20738044 indeed :D