Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing a part of datatemplate in a resource dictionary wpf

Tags:

wpf

I have a ResourceDictionary that lists different styles used in my wpf application.

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

etc...

Now how do I share the same StackPanel content across TemplateA and TemplateB in the same ResourceDictionary?

I apologize if Im missing something simple as this is my first pass learning WPF. Thanks

like image 346
theOne Avatar asked Mar 20 '23 02:03

theOne


1 Answers

Create a third DataTemplate and use a ContentPresenter to show that:

<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>
like image 154
Federico Berasategui Avatar answered May 06 '23 03:05

Federico Berasategui