首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重用HierarchicalDataTemplate?

如何重用HierarchicalDataTemplate?
EN

Stack Overflow用户
提问于 2013-07-26 14:54:04
回答 1查看 748关注 0票数 4

我有两个完全相同的HierarchicalDataTemplates。唯一的区别是模板的DataType。

代码语言:javascript
复制
<HierarchicalDataTemplate DataType="{x:Type Data:OuterType}"
                          ItemsSource="{Binding Items}">
    <StackPanel>...</StackPanel>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type Data:InnerType}"
                          ItemsSource="{Binding Items}">
    <StackPanel>...</StackPanel>
</HierarchicalDataTemplate>

如何避免在两个数据模板中重复堆栈面板的内容?

我考虑过将StackPanel变成用户控件,但这是唯一可以使用控件的地方。我希望StackPanel是某种资源,但我不知道如何使它工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-26 16:05:56

我会选择为那些看起来像这样的东西制作单独的模板:

代码语言:javascript
复制
<DataTemplate x:Key="sharedTemplate">
    <StackPanel>...</StackPanel>
</DataTemplate>

<HierarchicalDataTemplate DataType="{x:Type InnerType}"
                            ItemsSource="{Binding Items}">
    <ContentControl Content="{Binding}"
                    ContentTemplate="{StaticResource sharedTemplate}" />
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type OuterType}"
                            ItemsSource="{Binding Items}">
    <ContentControl Content="{Binding}"
                    ContentTemplate="{StaticResource sharedTemplate}" />
</HierarchicalDataTemplate>

在架构上有更优雅的解决方案,但是外观和感觉是由设计师处理的,我不喜欢在这些事情上使用过于复杂的编程范例。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17884643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档