0

나는 사용자 지정 WPF DependencyObject에 및 I에서 DependencyProperty에 대한 값 상속을 구현 노력하고있어 심하게 faling있어 :(WPF : 사용자 지정 DependencyObjects에 대한 가치 상속?

을 내가하고 싶은 것 :

나는 두 개의 클래스 T1이 0 디폴트 모두 DependencyProperty에 IntTest하고있다 T2, T1은 창이 포함 된 텍스트 상자에 논리적 루트/부모처럼 T2에 루트이어야한다.

그래서 의 값을 설정하지 않을 경우 T2.IntTestT1.IntTest 값을 제공해야합니다 (예 : TextBox.FlowDirection은 일반적으로 부모 창의 FlowDirection을 제공함).

내가 무슨 짓을 :

나는 FrameworkPropertyMetadataOptions.InheritsFrameworkPropertyMetadata를 사용하기 위해 FrameworkElement에서 파생, 두 클래스의 T1 및 T2를 만들었습니다. 또한 값 상속을 사용하려면 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;  
+0

어떤 가치를 얻으려고합니까? 상속받은 DP를 사용하지 않습니까? – user3455395

답변

0

연결된 속성의 핵심 속성은이 (일반적으로) 될 수 있음 그것이 설정되어있는 객체와 다른 객체에서 선언된다. 이 경우에는 T1에서 필요한 모든 것을 선언했지만 거기에서 멈춰야합니다 (IntTest 속성 래퍼 제거 - AP는 대신 Get/Set 메서드 사용). 다른 도우미 클래스에 IntTest을 선언 할 수도 있습니다. 신고 할 때마다 DependencyObject으로 설정할 수 있습니다.

// set on the declaring type 
T1.SetIntTest(t1, 123); 
// set on your other type 
T1.SetIntTest(t2, 456); 
// set on any other framework type 
Button button = new Button(); 
T1.SetIntTest(button, 789); 

다른 부분은 당신의 나무를 설정하는 것입니다. 상속은 논리적 계층 구조의 일부로 작동하므로 다른 인스턴스에서 값을 상속하려면 상속 객체가 원래 객체의 논리적 하위이어야합니다. 당신이베이스 FrameworkElement에서 파생되었으므로, 아이들 (single and multiple) 개념이 내장 된 ContentControlItemsControl에 의해 봉쇄의 이점을 얻지 못했습니다.