Notifications

Classes that represent entities such as customer or product generally contain public properties that describe the entities. The Customer class in Example 3-2 shows a simple entity that has a handful of public properties describing the customer. When a property’s value is changed, the entity does not communicate the change at all. If an instance of the entity is bound to a set of controls, the controls will not be notified or updated with the new values from the class instance.

Adding notifications to a class will enable the class to be able to raise events when its properties are changed, which in turn can be received by binding targets. The INotifyPropertyChanged interface is the device that carries out this mission.

Implementing the INotifyPropertyChanged Interface

The next set of examples will demonstrate how to make an entity support the INotifyPropertyChanged interface and hook into XAML-based binding modes. Example 3-5 shows the Product class written in C# using automatic properties. Using automatic properties restricts the property accessors to having the simplest getter and setter code that merely gets and sets the values. You would have to modify this code to use formal getters and setters to implement the INotifyPropertyChanged interface.

Example 3-5. The Product class

C# public class Product { public int ProductId { get; set; } public string ProductName { get; set; } public int QuantityPerUnit { get; set; } public decimal UnitPrice { get; set; } public ...

Get Data-Driven Services with Silverlight 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.