在资源字典wpf中共享数据模板的一部分



我有一个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>

等等……

现在我如何在相同的资源词典中跨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>

最新更新