ViewModel and HelperClasses with INotifyPropertyChanged

This is quite a known fact and most seasoned WPF/Silverlight guys know well about it. But for those newbie trying their hand at WPF/Silverlight, please note the following:

If you are using helper classes within your viewmodel then those helper classes themselves need to implement INotifyPropertyChanged to allow any changes to their internal state be visible on UI.


You have an employee class with public Name and public ID. This class is a member variable in your ViewModel. You have declared it as public Emp _emp = new Emp(“John”, 11);

You will have a public property on view model that will expose this Employee to be bindable to view – something like

public Employee MyEmployee

{return _emp; }
set{_emp = value;NotifyPropertyChanged(“MyEmployee”)}

And then somewhere in your code you change the contents of this employee object. This change is done in the viewmodel, not form UI. So say you change the _emp object to set name = “smith”, id = “22”. When you do this,  as far as ViewModel is concerned, it will not hit the set block of your MyEmployee property because the object is still the same i.e. its reference has not changed and it still points to the same heap address, but its contents have changed! However, you will not see these changes getting reflected on the UI. So in order for this to be visible across the viewmodel AND UI, your Emp must implement INotifyPropertyChanged. After doing this, you will see that any changes to the contents of Emp class will raise proper notifications and ViewModel & UI will remain aware of these changes – reacting to it as needed.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s