I'd like to create a re-usable ListView with rows of any or all of:
Labels + textboxes or Labels + Comboboxes or Labels + DatePickers
using Templates. I still do not understand Templates too well and would like to know which of them - ControlTemplate, DataTemplate, ItemsTemplate or ContentTemplate - to use for this and how. Thanks!
There are 2 different types of templates: DataTemplate
and ControlTemplate
. ControlTemplate
is used on the Template
property of classes derived from Control
and defines the visual tree for a specific type of control.
Pretty much any other place that templates show up is using DataTemplate
. This includes ContentTemplate
and ItemTemplate
properties. DataTemplates
define a visual tree for any non-Visual data type. When the template is rendered its DataContext
is the data object being rendered (i.e. a List<T>
item) making it easy to bind data properties.
To mix templates for different types in a single list you can use a DataTemplateSelector
which allows you to write code to pick a template for each item. The other option is to create multiple implicit templates (DataType
but no x:Key
) for the different CLR types of objects in the list. As long as those templates are in the resource scope of the control rendering the collection the types will resolve their templates automatically.