DataTemplate vs. ControlTemplate
Hello,
I'm not so sure that I understand the difference between a DataTemplate and ControlTemplate.
I realize that the DataTemplate renders a 'type' of data. But then why is ColumnHeaderTemplate defined using a DataTemplate resource?
Thanks
Houman
Answers
They're mostly the same, getting most of their behavior from the base FrameworkTemplate class. The primary difference is that ControlTemplate has a TargetType property and DataTemplate has a DataType property.
Another difference is that the DataTemplateSelector can be used to choose a DataTemplate at runtime. So, for example, the ColumnHeaderTemplate that you mentioned has a companion ColumnHeaderTemplateSelector property.
here is one from the samples
http://windowssdk.msdn.microsoft.com/en-us/library/ms771489.aspx
All Replies
They're mostly the same, getting most of their behavior from the base FrameworkTemplate class. The primary difference is that ControlTemplate has a TargetType property and DataTemplate has a DataType property.
Another difference is that the DataTemplateSelector can be used to choose a DataTemplate at runtime. So, for example, the ColumnHeaderTemplate that you mentioned has a companion ColumnHeaderTemplateSelector property.
Thanks Mike,
I'm still trying to figure out when to use what...but it is getting easier...
Houman
Wow I've never thought about it this way. But plain and simple, whenever you want a control template to be assigned dynamically, you need DataTemplates along with a DataTemplateSelector.
For example, my grid control uses a datatemplate selector that looks at the type of cell (Text, Image, DropDown, CheckBox) and assigns the appropriate DataTemplate.
In cases where the control will never change, just use a controltemplate and be done with it

Thanks Mike, that makes sense.
Is there a good example out there on dataTemplateSelector sounds like a very powerful feature.
Thanks
Houman
here is one from the samples
http://windowssdk.msdn.microsoft.com/en-us/library/ms771489.aspx
- Thanks Lee

