Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Notify Clients when Changes Happen (http://www.go4expert.com/forums/notify-clients-changes-happen-t21340/)

arunlalds 13Mar2010 15:12

Notify Clients when Changes Happen
 
Implement the INotifyPropertyChanged interface (located in
Code:

System.ComponentModel).
using System.ComponentModel;
...
class MyDataClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
}
private int _tag = 0;
public int Tag
{
get
{ return _tag; }
set
{
_tag = value;
OnPropertyChanged(“Tag”);
}
}
}

The Windows Presentation Foundation (WPF) makes extensive use of this interface for
data binding, but you can use it for your own purposes as well.
To consume such a class, use code similar to this:
Code:

void WatchObject(object obj)
{
INotifyPropertyChanged watchableObj = obj as INotifyPropertyChanged;
if (watchableObj != null)
{
watchableObj.PropertyChanged += new
PropertyChangedEventHandler(data_PropertyChanged);
}
}
void data_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
//do something when data changes
}



All times are GMT +5.5. The time now is 22:03.