Use the same DataTemplate for different types

advertisements

Is it possible to use the same DataTemplate for a defined selection of types, i.e. how to change the following sample code so that the same DataTemplate is used for all listed types?

<DataTemplate DataType="{x:Type local:ClassA, ClassB, ...}">
   <StackPanel Orientation="Horizontal">
      <Label Content="{Binding Path=Title}"/>
      ...
   </StackPanel>
</DataTemplate>


This isn't supported by default, but typically I put the contents of the DataTemplate in a UserControl or another DataTemplate (depends on how complex the template is), and just write a 3-line data template for each class item

<UserControl x:Class="MyUserControl">
   <StackPanel Orientation="Horizontal">
      <Label Content="{Binding Path=Title}"/>
      ...
   </StackPanel>
</UserControl >

<DataTemplate DataType="{x:Type local:ClassA}">
    <local:MyUserControl />
</DataTemplate>
<DataTemplate DataType="{x:Type local:ClassB}">
    <local:MyUserControl />
</DataTemplate>
<DataTemplate DataType="{x:Type local:ClassC}">
    <local:MyUserControl />
</DataTemplate>