WPF DependencyProperties vs INotifyPropertyChanged
2011-09-23
There are 2 ways to get the automatic notice target object if the source object changed (for example: the value) when you using WPF data binding: the one is Dependency Properties, the other one is INotifyPropertyChanged:
INotifyPropertyChanged sample:
public class IODataPresentation : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;_
private string _ValueString;_
public string ValueString
{
get { return _ValueString; }
set
{
_ValueString = value;_
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("ValueString"));
}
}
}
public IODataPresentation() { }
}
DependencyProperties sample:
// Dependency Property
public static readonly DependencyProperty NewValueStringProperty =
DependencyProperty.Register("ValueString", typeof(string),
typeof(UDirectIOBlock), new FrameworkPropertyMetadata(""));_
_public string NewValueString
{
get { return (string)GetValue(NewValueStringProperty); }
set { SetValue(NewValueStringProperty, value); }
}
About their differences, please read 2 great articles here and here.