views:

15

answers:

1

I have a class that inherits from DrawingVisual. It declares a DependencyProperty which, when registered, should inherit its value from a parent.

I then create a parent-child relationship between two instances of this class. I set the parent DependencyProperty but the child's DependencyProperty does not return the parent's value. Can anyone determine what I'm doing wrong?

Here's the declaration for the DrawingVisual sub-type:

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); }
    }
}

The method that creates the parent-child relationship is as follows:

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
}

Any help greatly appreciated, thanks.

A: 

FrameworkPropertyMetadata only works for FrameworkElement-derived classes.

I reproduced your problem on a VS-generated WPF app, then derived MyDrawingVisual from FrameworkElement and it works as advertised. If it derives from UIElement (the base class of FrameworkElement), it no longer works.

Visual inheritance depends on the internal property InheritanceParent, which we cannot set, sorry.

Timores
Good spot, thanks.
Chris