我有一个 ResourceDictionary,它列出了我的 wpf 应用程序中使用的不同样式。
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="TemplateA">
<Some A specific definitions />
<StackPanel>
<!-- Same content as other templates -->
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="TemplateB">
<Some B specific definitions />
<StackPanel>
<!-- Same content as other templates -->
</StackPanel>
</DataTemplate>
等等...
现在如何在同一个 ResourceDictionary 中的 TemplateA 和 TemplateB 之间共享相同的 StackPanel 内容?
如果我遗漏了一些简单的东西,我深表歉意,因为这是我第一次学习 WPF。
谢谢
最佳答案
创建第三个 DataTemplate 并使用 ContentPresenter
来显示:
<DataTemplate x:Key="SharedPart">
<StackPanel>
<!-- Same content as other templates -->
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="TemplateA">
<Some A specific definitions />
<ContentPresenter Content="{Binding}"
ContentTemplate="{StaticResource SharedPart}"/>
</DataTemplate>
<DataTemplate x:Key="TemplateB">
<Some B specific definitions />
<ContentPresenter Content="{Binding}"
ContentTemplate="{StaticResource SharedPart}"/>
</DataTemplate>