[WPF Property Changes] What is the correct way to hook into a WPFproperty change. -
12-27-2007
, 01:14 PM
In the "old days" of Windows Forms, if you had a control with, say, a
DataSource property and wanted to perform some action when the
property changed, you would simply put your logic right into the
property accessor. Now, with WPF and DependencyProperties, it doesn't
seem "right" to do this anymore. The only solution that seems to work
for me is as follows, but I'm not sure that it's right. Could somebody
that really understands what's going on here provide some
enlightenment?
//Constructor
pubic Test()
{
DependencyPropertyDescriptor.FromProperty(MyProper tyProperty,
typeof(Test)).AddValueChanged(this, delegate(object sender, EventArgs
e){/*Logic that you would previously have in your set accessor goes
here*/});
}
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(Test),
typeof(string));
public string MyProperty
{
get {return (string)GetValue(MyPropertyProperty);}
set {SetValue(MyPropertyProperty, value);}
}
-Alan |