如何在MyDP1_Changed和MyDP1_Changed中禁用调用回调方法? 当我从其他回调方法
更改值时,我需要可调回调class MyDependencyObject : DependencyObject
{
public object MyDP1 {
get{ return GetValue(MyDP1Property); }
set{ return SetValue(MyDP1Property, value); }
}
public object MyDP2 {
get{ return GetValue(MyDP2Property); }
set{ return SetValue(MyDP2Property, value); }
}
public static readonly DependencyProperty MyDP1Property =
DependencyProperty.Register("MyDP1", typeof(object), typeof(MyDependencyObject )
, new UIPropertyMetadata(MyDP1_Changed));
public static readonly DependencyProperty MyDP2Property =
DependencyProperty.Register("MyDP2", typeof(object), typeof(MyDependencyObject )
, new UIPropertyMetadata(MyDP2_Changed));
private static void MyDP1_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
d.SetValue(MyDP2Property, e.NewValue);
}
private static void MyDP2_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
d.SetValue(MyDP1Property, e.OldValue);
}
}
答案 0 :(得分:1)
首先,您需要使用SetCurrentValue
而不是SetValue
- 否则您将破坏绑定。
其次,您可以使用这些方法临时添加和删除回调
TypeDescriptor.GetProperties(d)["MyDP1"].RemoveValueChanged(d, MyDP1_Changed)
TypeDescriptor.GetProperties(d)["MyDP1"].AddValueChanged(d, MyDP1_Changed)