代码之家  ›  专栏  ›  技术社区  ›  Gishu

如何在WPF中编写数据模板/链接到子数据模板?

  •  2
  • Gishu  · 技术社区  · 14 年前

    问题来了。

    现在我需要在另一个地方重用上面Datatemplate的Properties部分。原因:为避免冗余,请确保对datatemplate的进一步更新以相同的方式应用于所有用途。

    我找到了一个办法。。但我不确定这是正确的还是最好的。。把它作为一个答案贴在下面,这样就可以给它打分了。

    1 回复  |  直到 14 年前
        1
  •  3
  •   Gishu    14 年前

    我使用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>