Wpf 如何绑定到依赖项对象的依赖项属性?

Wpf 如何绑定到依赖项对象的依赖项属性?,wpf,binding,dependency-properties,Wpf,Binding,Dependency Properties,以下只是问题核心的总结 我定义了具有一个依赖属性的类,如下所示 public class TestMap : DependencyObject { public bool TestProperty { get { return (bool)GetValue(TestPropertyProperty); } set { SetValue(TestPropertyProperty, value); } } public static

以下只是问题核心的总结

我定义了具有一个依赖属性的类,如下所示

public class TestMap : DependencyObject
{
    public bool TestProperty
    {
        get { return (bool)GetValue(TestPropertyProperty); }
        set { SetValue(TestPropertyProperty, value); }
    }

    public static readonly DependencyProperty TestPropertyProperty =
        DependencyProperty.Register("TestProperty", typeof(bool), typeof(TestMap), new PropertyMetadata(false));
}
现在我将在UserControl中使用上面的类。 下面的代码是一个示例

public class UserControl1 : Control
{
    public TestMap TestMap { get; set; }
}
我在主窗口中使用UserControl1,如下所示

<Window>
    <Grid>
        <local:UserControl1 />
    </Grid>
</Window>

上面的代码工作得很好,但是如果我尝试绑定到TestMap.TestProperty,我应该怎么做? 我尝试了下面的代码,但不起作用。(假设ViewModel已绑定)


上述代码的错误消息如下图所示

错误消息是 “在“TestMap”格式中找不到可连接的“TestProperty”属性。”


感谢阅读。

您不能绑定到嵌套属性,即属性
TestMap
返回的对象的属性
TestProperty
,如XAML中的这样。它不受支持


您应该将<代码> TestFase移到<代码> UserControl < />代码,以编程方式设置它,或者考虑使用可在特定类型的任何对象上设置的值。

不能绑定到XAML中的嵌套属性。另外,语法不受支持,<>代码> TESTMAP显然是“代码> null ”,因此没有设置绑定的对象。
TestProperty
不在
UserControl1
中的任何原因?谢谢您的回答。
<Window>
    <Grid>
        <local:UserControl1 TestMap.TestProperty="{Binding ViewModelProperty}"/>
    </Grid>
</Window>