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
andControlTemplate
.ControlTemplate
is used on theTemplate
property of classes derived fromControl
and defines the visual tree for a specific type of control.Pretty much any other place that templates show up is using
DataTemplate
. This includesContentTemplate
andItemTemplate
properties.DataTemplates
define a visual tree for any non-Visual data type. When the template is rendered itsDataContext
is the data object being rendered (i.e. aList<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 nox: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.