我有一个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>
Run Code Online (Sandbox Code Playgroud)
等等...
现在,如何在同一ResourceDictionary中跨TemplateA和TemplateB共享相同的StackPanel内容?
如果我错过了一些简单的东西,我很抱歉,因为这是我第一次学习WPF.谢谢
创建第三个DataTemplate并使用a 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>
Run Code Online (Sandbox Code Playgroud)