我想在每次更改属性时执行一些代码。以下在一定程度上起作用:
public partial class CustomControl : UserControl
{
public bool myInstanceVariable = true;
public static readonly DependencyProperty UserSatisfiedProperty =
DependencyProperty.Register("UserSatisfied", typeof(bool?),
typeof(WeeklyReportPlant), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnUserSatisfiedChanged)));
private static void OnUserSatisfiedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Console.Write("Works!");
}
}
当 UserSatisfiedProperty 的值更改时,这将打印“Works”。问题是我需要访问调用 OnUserSatisfiedChanged 的 CustomControl 实例来获取 myInstanceVariable 的值。我怎样才能做到这一点?