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.
How to Use DependencyObject
- Check whether the System.Windows namespace is imported in your application. If not then import the System.Windows namespace.
using System.Windows;
- Inherit the DependencyObject class in to your entity.
public class Person : DependencyObject
{
}
- Create objects of the DependencyProperty class. The number of objects should be equal to the number of your properties in your entity. For example if you have 3 properties then the objects must have 3. The object should be static.
Example:
public static DependencyProperty NameProperty;
- Declare a constructor of your entity class and in the constructor register all the properties with Dependency property objects. The following code calls the Register method of the Dependency property class to do the registration. It takes three parameters, the name of the parameters are in a string, the datatype of the parameter and the type of the class the property belongs to.
Example:
NameProperty= DependencyProperty.Register("Name",typeof(string),typeof(Person));
AgeProperty= DependencyProperty.Register("Age",typeof(int),typeof(Person));
- In the geter and seter method of your property call the getValue andSetValue of the base class, I mean the DependencyObject class.
Example:
privatestring name;
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty,value); }
}
private string age;
public string Age
{
get { return (int)GetValue(NameProperty); }
set { SetValue(NameProperty,value); }
}
Summary
In the example above we have seen how to use InotifyPropertyChanged and the DependencyObject class to get notification in the client when the source changes.