I have many items inside a list control. I want each item to have a different item template depending on the type of the item. So the first item in the list is a ObjectA type and so I want it to be rendered with ItemTemplateA. Second item is a ObjectB type and so I want it to have ItemTemplateB for rendering. At the moment I can only use the ItemTemplate setting to define one template for them all. Any way to achieve this?
the ItemTemplateSelector
will work but I think it is easier to create multiple DataTemplate
s in your resource section and then just giving each one a DataType
. This will automatically then use this DataTemplate
if the items generator detects the matching data type?
<DataTemplate DataType={x:Type local:ObjectA}>
...
</DataTemplate>
Also make sure that you have no x:Key
set for the DataTemplate
.
Read more about this approach here