If an object implementing INotifyPropertyChanged Interface it’ll raise a property changed event when its property changes.Lets create a sample application to know how we can implement INotifyPropertyChanged Interface. I am creating a Silverlight application which demonstrates the data binding with both an object implementing INotifyPropertyChanged and also a normal DependencyProperty.
- Open Visual Studio and select a new silverlight application.
- Create a class named Customer in the silverlight project and implement INotifyPropertyChanged.
- Define INotifyPropertyChanged Members,
- In property setter invoke OnPropertyChanged by passing property name like,
In MainPage.xaml.cs add an ObservableCollection of customer object as Dependency property inorder to make sure that UI is updating while we assigning that customer list to another list or object.If we are making it as a normal property UI will update only if we add new object to customerlist or any change occurs to the underlying properties.
- I also added a DependencyProperty FirstName in MainPage.xaml.cs just to show the binding of a simple DependencyProperty.
- In MainPage.XAML add a datagrid and textbox and bind it to the ObservableCollection and DependencyProperty respectively.
- For Understanding PropertyChanged event, I added a button and just updating the customer object in the click event so that you can see the changes in the datagrid.When you change the property of Customer object from click event you can see that the UI is updating accordingly.
- Download Sample Application.