【问题标题】:DrawingVisual DependencyProperty not Inheriting from ParentDrawingVisual DependencyProperty 未从父级继承
【发布时间】: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


    【解决方案1】:

    FrameworkPropertyMetadata 仅适用于 FrameworkElement 派生类。

    我在 VS 生成的 WPF 应用程序上重现了您的问题,然后从 FrameworkElement 派生了 MyDrawingVisual,它的工作原理与宣传的一样。如果它派生自 UIElement(FrameworkElement 的基类),则不再有效。

    视觉继承依赖于内部属性 InheritanceParent,我们无法设置,抱歉。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-04
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多