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?
Wpf – Different item template for each item in a WPF List
itemtemplatewpf
Best Solution
the
ItemTemplateSelector
will work but I think it is easier to create multipleDataTemplate
s in your resource section and then just giving each one aDataType
. This will automatically then use thisDataTemplate
if the items generator detects the matching data type?Also make sure that you have no
x:Key
set for theDataTemplate
.Read more about this approach here