Introduction
This article explains how bound controls are updated when the source is changed. You may encounter the problem of displaying updates when the source is changed. This problem can be overcome using the INotifyPropertyChanged Interface or DependencyObject.
Let us have a little description of the INotifyPropertyChanged interface and DependencyObject.
INotifyPropertyChanged
The InotifyPropertyChanged interface is in the System.ComponentModel Namespace. It is used to notify clients bound by your data source when you make changes to your data source.
For example,
Your are storing your employing details, such as name, id, address and so on. If you only bound your entity to controls without using the InotifyPropertyChanged interface and did not implement their events in your entity class then your controls will not be updated if you change the source. In other words if you change the name, id and address in runtime (not using the controls, change by code), the changes will not display in the control.
DependencyObject
DependencyObject is a class in the System.Windows Names namespace. It is also used for client notification. For more details please open the link 'http://msdn.microsoft.com/en-us/library/system.windows.dependencyobject.aspx '
Let's start the description of the use of the InotifyPropertyChanged interface and their implementation. Use the following procedure to use the InotifyPropertyChanged interface.
How to impliment INotifyPropertyChanged
- Import the System.ComponentModel namespace for your entity.
using System.ComponentModel;
- Impliment the InotifyPropertyChanged interface in your entity,
class employee : INotifyPropertyChanged
{
}
- Right-click on the InotifyPropertyChanged interface and go to implimentInterface, then click on "Implement Interface Explicitly".
- The PropertyChanged EventHandler will be automatically added to your entity.
public event PropertyChangedEventHandler PropertyChanged;
- Declare the function or method that will call the constructor of the PropertyChanged EventHandler. The function you have declared should have a parameter having data type string,
for example:
public void OnPropertyChanged(string name){
if (PropertyChanged !=null)
PropertyChanged(this,new PropertyChangedEventArgs(name));
}
- The property Changed EventHandler takes two parameters (object sender and argument), the first parameter is the property that will be changed and the second is the name of the property of which the value will be changed.
In the geter and seter method of your property call the function OnPropertyChanged and pass the name of the property as a parameter.
private string firstname;
public string FirstName
{
get
{
return firstname;
}
set
{
firstname =value;
OnPropertyChanged("FirstName");
}
}
private int age;
public int Age
{
get
{
return age;
}
set
{
age =value;
OnPropertyChanged("Age");
}
}
- Test the notification to ensure it will work successfully. Than let's proceed to the how to use DependencyObject.
Join the conversation! Your thoughts help the community grow.