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

标签 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>

等等...

现在如何在同一个 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>

关于wpf - 在资源字典 wpf 中共享数据模板的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22643857/

相关文章:

c# - 如何更改列标题文本大小

wpf - XAML、WPF 和 Windows 8

c# - StackPanel 高度超过父 Grid 高度

wpf - 通过代码设置带有WPF的超链接的值

c# - 带有叠加项目控件的图像

wpf - 在 WPF 中使用 FindName() 找不到动态生成的控件

c# - 如何将转换器应用于 ObservableCollection 的每个项目?

wpf - 如何基于 2 个或更多 WPF 文本框填充 WPF 标签?

wpf - 在多个 WPF 控件之间共享列宽

c# - 如果属性值没有变化,是否可以从 IValueConverter 进行 "cancel"依赖属性赋值?