2014-03-25 34 views
3

我有一個ResourceDictionary列出了我的wpf應用程序中使用的不同樣式。在資源字典中共享datatemplate的一部分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。 感謝

回答

4

創建第三個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> 
+0

謝謝HighCore! – theOne