【发布时间】:2010-06-28 11:05:32
【问题描述】:
我有一个继承自 DrawingVisual 的类。它声明了一个 DependencyProperty,它在注册时应该从父级继承其值。
然后我在这个类的两个实例之间创建父子关系。我设置了父 DependencyProperty,但子的 DependencyProperty 不返回父的值。谁能确定我做错了什么?
这是 DrawingVisual 子类型的声明:
public class MyDrawingVisual : DrawingVisual
{
public static readonly DependencyProperty SomeValueProperty;
static MyDrawingVisual()
{
FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata() { Inherits = true };
SomeValueProperty = DependencyProperty.Register("SomeValue", typeof(double), typeof(MyDrawingVisual), metadata);
}
public double SomeValue
{
get { return (double)GetValue(SomeValueProperty); }
set { SetValue(SomeValueProperty, value); }
}
}
创建父子关系的方法如下:
private void Test()
{
MyDrawingVisual mdv1 = new MyDrawingVisual() { SomeValue = 1.23 };
MyDrawingVisual mdv2 = new MyDrawingVisual();
mdv1.Children.Add(mdv2);
double value = mdv2.SomeValue; // Expected = 1.23, actual = 0.0
}
非常感谢任何帮助,谢谢。
【问题讨论】:
标签: wpf inheritance dependency-properties