2010-06-28 31 views
1

繼承我有一個從DrawingVisual繼承的類。它聲明瞭一個DependencyProperty,它在註冊時應該從父類繼承它的值。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 
} 

任何幫助非常感謝,謝謝。

回答

1

FrameworkPropertyMetadata僅適用於FrameworkElement派生類。

我複製你的問題在VS生成的WPF應用程序,然後自FrameworkElement派生MyDrawingVisual,它像宣傳的那樣。如果它來自UIElement(FrameworkElement的基類),它將不再起作用。

視覺繼承依賴於內部屬性InheritanceParent,這是我們不能設置,對不起。

+0

好的地方,謝謝。 – Chris 2010-06-28 12:02:48