Reputation:
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>
Upvotes: 6
Views: 2681
Reputation: 132548
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>
Upvotes: 10
Reputation: 5559
It's not supported out of the box, but it would be possible to do something like this by defining custom MarkupExtension. Similar to x:Type extension.
If here, ClassA, ClassB are deriving from same class you should be able to put the base class name here to refer them all.
Upvotes: 1