나는 사용자 지정 WPF DependencyObject에 및 I에서 DependencyProperty에 대한 값 상속을 구현 노력하고있어 심하게 faling있어 :(WPF : 사용자 지정 DependencyObjects에 대한 가치 상속?
을 내가하고 싶은 것 :
나는 두 개의 클래스 T1이 0 디폴트 모두 DependencyProperty에 IntTest하고있다 T2, T1은 창이 포함 된 텍스트 상자에 논리적 루트/부모처럼 T2에 루트이어야한다.
그래서 의 값을 설정하지 않을 경우 T2.IntTest은 T1.IntTest 값을 제공해야합니다 (예 : TextBox.FlowDirection은 일반적으로 부모 창의 FlowDirection을 제공함).
내가 무슨 짓을 :
나는 FrameworkPropertyMetadataOptions.Inherits와 FrameworkPropertyMetadata를 사용하기 위해 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;
어떤 가치를 얻으려고합니까? 상속받은 DP를 사용하지 않습니까? – user3455395