WPF Control的嵌套属性的数据绑定

我正在尝试使用一些嵌套属性来开发用户控件,这些属性允许使用数据绑定来设置它。 例如,我有这样的事情:

// Top level control public class MyControl : Control { public string TopLevelTestProperty { get { return (string)GetValue(TopLevelTestPropertyProperty); } set { SetValue(TopLevelTestPropertyProperty, value); } } public static readonly DependencyProperty TopLevelTestPropertyProperty = DependencyProperty.Register("TopLevelTestProperty", typeof(string), typeof (MyControl), new UIPropertyMetadata("")); // This property contains nested object public MyNestedType NestedObject { get { return (MyNestedType)GetValue(NestedObjectProperty); } set { SetValue(NestedObjectProperty, value); } } public static readonly DependencyProperty NestedObjectProperty = DependencyProperty.Register("NestedObject", typeof(MyNestedType), typeof (MyControl), new UIPropertyMetadata(null)); } // Nested object's type public class MyNestedType : DependencyObject { public string NestedTestProperty { get { return (string)GetValue(NestedTestPropertyProperty); } set { SetValue(NestedTestPropertyProperty, value); } } public static readonly DependencyProperty NestedTestPropertyProperty = DependencyProperty.Register("NestedTestProperty", typeof(string), typeof (MyNestedType), new UIPropertyMetadata("")); } // Sample data context public class TestDataContext { public string Value { get { return "TEST VALUE!!!"; } } } ... this.DataContext = new TestDataContext(); ... 

XAML:

       

它适用于属性TopLevelTestProperty,但它不适用于NestedTestProperty。 似乎嵌套绑定不起作用。 有人可以帮帮我吗? 有没有办法做出这样的约束? 我认为这是因为我的嵌套对象没有任何对顶级对象的引用,因此无法使用MyControl的DataContext解析它。

HB右,嵌套控件不从mycontrolinheritanceDataContext。 Tyr out明确地设置它: