简体   繁体   English

如何在XAML中引用另一个依赖项属性?

[英]How to refer to another dependency property in XAML?

I have a UserControl with two DataTemplate dependency properties: Template1 and Template2 . 我有一个具有两个DataTemplate依赖项属性的UserControlTemplate1Template2 I want to render the template in Template2 via a ContentControl in Template1 . 我想通过Template2ContentControl呈现Template2中的Template1 How do I bind to this? 我该如何绑定? I tried using the following, but got binding exceptions: 我尝试使用以下内容,但遇到绑定异常:

<ContentControl ContentTemplate="{Binding RelativeSource={RelativeSource Self}, Path=Template2}" />

Full code: 完整代码:

<MyUserControl>
    <MyUserControl.Template1>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="This is in Template1!" />
                <ContentControl ContentTemplate="???" />
            </StackPanel>
        </DataTemplate>
    </MyUserControl.Template1>
    <MyUserControl.Template2>
        <DataTemplate>
            <TextBlock Text="This is in Template2!" />
        </DataTemplate>
    </MyUserControl.Template2>
</MyUserControl>

You can try something like this. 您可以尝试这样。

<Window.Resources>
    <DataTemplate x:Key="Template2">
        <TextBlock Text="This is in Template2!" />
    </DataTemplate>
    <DataTemplate x:Key="Template1">
        <StackPanel>
            <TextBlock Text="This is in Template1!" />
            <ContentControl ContentTemplate="{StaticResource Template2}" />
        </StackPanel>
    </DataTemplate>
</Window.Resources>

<MyUserControl ContentTemplate="{StaticResource Template1}">

</MyUserControl>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM