如何在WPF中编写DataTemplates / link到子datatemplate?

时间:2010-05-18 11:06:11

标签: wpf datatemplate

这是问题所在 给定一个大/复杂的数据模板A,它有3个部分 - General,Properties,Misc。 想象一下每个3个网格 现在我需要在另一个地方重用上面的Datatemplate的Properties部分。理由:为避免冗余,请确保对数据模板的进一步更新应用于所有用途。

所以我想我要求的是能够在父Datatemplate中插入到子DataTemplate的链接。什么是最好的方法呢?

我找到了一种方法可以做到这一点..但我不确定它是正确的方式还是最好的方法。将它作为下面的答案发布,以便它可以被评级。

1 个答案:

答案 0 :(得分:3)

我使用ContentPresenter通过其ContentTemplate属性插入子datatemplate。

// child
<DataTemplate x:Key="propertiesVMTemplate">
    <toolkit:DataGrid Style= ....  // lots of stuff here
    </toolkit:DataGrid>
</DataTemplate>

// parent
<DataTemplate x:Key="nodeVMTemplate">
    ... general section
        // and the link
        <ContentPresenter Content="{Binding Properties}" ContentTemplate="{StaticResource propertiesVMTemplate}"/>

        ...misc section stuff
</DataTemplate>