How do you know INotifyPropertyChanged is Working

Today I had the great enjoyment to make two presentations to the Microsoft Health and Life Sciences Conference here in Atlantic City.

While showing DataBinding, I became interested in demonstrating the effects of INotifyPropertyChanged, The basic mechanism is this; you add a PropertyChangedEventHandler to your business object and each time you set a bound property you invoke that event, passing in a reference to the business object and an instance of PropertyChangedEventArgs initialized with the name of the property

PropertyChanged

The bound Silverlight controls will respond to this event and update themselves to the new value. But how to test this?

We tried making a new browser, but no go; we suspected (correctly) that the UI was pointing to two different objects. I proved this to myself by modifying the constructor to generate a random number and store it in a member variable, which I display.

AddedID

 

When you attempt to create two views on the data (by hitting control N on the browser or by copying the URL to a new tab) you generate a new object as you can see by the new ID.

 

SideBySide

 

To ensure that we're seeing the UI update in response to changes to the same object, it is easiest to just add two new rows, one that shows the value of IsPublished and the other that shows Quantity on Hand. When you change the original, the duplicate should be informed through the event.

ReflectionOfBinding

 

We can now run the program again and when we change the isPublished or QuantityOnHand properties, we should see that immediately reflected in the second control. Turn off the notification and the reflection should stop.

 

ReflectedBindingRunning

Share

About Jesse Liberty

Jesse Liberty is an independent consultant and programmer with three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP, Microsoft MVP and Telerik MVP.
This entry was posted in z Silverlight Archives. Bookmark the permalink.

Leave a Reply

Your email address will not be published.