我正在尝试在自定义 WPF DependencyObject 中为 DependencyProperty 实现值继承,但我失败了:(
我想做的事:
我有两个类T1和T2都有一个 DependencyProperty IntTest,默认为 0。T1 应该是 T2 的根,就像 Window 是它包含的 TextBox 的逻辑根/父级一样。
因此,当我没有明确设置T2.IntTest的值时,它应该提供T1.IntTest的值(就像 TextBox.FlowDirection 通常提供父窗口的 FlowDirection 一样)。
我做了什么:
我创建了两个类 T1 和 T2,从 FrameworkElement 派生,以便将FrameworkPropertyMetadata与FrameworkPropertyMetadataOptions.Inherits一起使用。我还读到,为了使用值继承,您必须将 DependencyProperty 设计为 AttachedProperty。
目前,当我为根 T1 赋值时,子 T2 的 DP-Getter 不会返回它。
我究竟做错了什么???
这是两个类:
// Root class
public class T1 : FrameworkElement
{
// Definition of an Attached Dependency Property
public static readonly DependencyProperty IntTestProperty = DependencyProperty.RegisterAttached("IntTest", typeof(int), typeof(T1),
new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.Inherits));
// Static getter for Attached Property
public static int GetIntTest(DependencyObject target)
{
return (int)target.GetValue(IntTestProperty);
}
// Static setter for Attached Property
public static void SetIntTest(DependencyObject target, int value)
{
target.SetValue(IntTestProperty, value);
}
// CLR Property Wrapper
public int IntTest
{
get { return GetIntTest(this); }
set { SetIntTest(this, value); }
}
}
// Child class - should inherit the DependenyProperty value of the root class
public class T2 : FrameworkElement
{
public static readonly DependencyProperty IntTestProperty = T1.IntTestProperty.AddOwner(typeof(T2),
new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.Inherits));
public int IntTest
{
get { return (int)GetValue(IntTestProperty); }
set { SetValue(IntTestProperty, value); }
}
}
这是试用它的代码:
T1 t1 = new T1();
T2 t2 = new T2();
// Set the DependencyProperty of the root
t1.IntTest = 123;
// Do I have to build a logical tree here? If yes, how?
// Since the DependencyProperty of the child was not set explicitly,
// it should provide the value of the base class, i.e. 123.
// But this does not work: i remains 0 :((
int i = t2.IntTest;