Different item template for each item in a WPF List? Different item template for each item in a WPF List? wpf wpf

Different item template for each item in a WPF List?


the ItemTemplateSelector will work but I think it is easier to create multiple DataTemplates 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


Have a look at the ItemTemplateSelector property of your list control. You can point it to a custom TemplateSelector and decide which template to use in code.

Here's a blog post describing TemplateSelectors:

http://blogs.interknowlogy.com/johnbowen/archive/2007/06/21/20463.aspx

Edit: Here's a better post:

http://blog.paranoidferret.com/index.php/2008/07/16/wpf-tutorial-how-to-use-a-datatemplateselector/