[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5629
  • Last Modified:

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

0
vkarumbaiah
Asked:
vkarumbaiah
  • 5
  • 4
2 Solutions
 
p_davisCommented:
private PropertyChangedEventHandler propertyChanged;
        public event PropertyChangedEventHandler PropertyChanged
        {
            add { this.propertyChanged += value;}
       
        }
0
 
PockyMasterCommented:
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?
0
 
vkarumbaiahAuthor 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.  
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
PockyMasterCommented:
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
0
 
PockyMasterCommented:
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
0
 
vkarumbaiahAuthor 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;}
       
        }
0
 
vkarumbaiahAuthor 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
0
 
vkarumbaiahAuthor 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
0
 
PockyMasterCommented:
exactly like 20738044 indeed :D
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now