Monday, March 21, 2011

INotifyPropertyChanged

In WPF we write the ViewModel class, we can get the class inherited from dependencyObject and register the class property as DependencyProperty. But Silverligth doesn't support this.

So the most common scenario is to let the class inherited from INotifyPropertyChanged interface, but when implement the interface, let's follow strictly with the sample code in MSDN.

for instance:

public class MyVMClass: INotifyPropertyChanged
{
private string length;

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}

public string Length
{
get {return this.length;}
set
{
if(value!=this.length)
{
this.length=value;
NotifyPropertyChanged("Length");
}
}
}

Notice the if statement is the property set block is important. that is only the value is different, the propertyChanged event is triggered.

No comments: