ItemTemplate vs ControlTemplate

A ControlTemplate let's you change the look&feel of existing controls, for example, make a normal everyday button look like Vista's taskbar shiny glass buttons or make a tab control look like the new Office 2007 ribbon.

The idea is that the control contains only the behavior and the ControlTemplate contains the look&feel - so a button is "something you click" not "a rectangle with some 3d effect you can click on".

You can replace the ListBox's control template to get something like a list box with rounded corners or custom scroll bars.

A DataTemplate (ItemTemplate is a property of type DataTemplate) on the other hand let's you control how the data rows displayed inside list boxes (and other similar controls), data templates are used to specify things like "Show last name, than a comma and the first name - and the last name should be bold and the first name should be green if it starts with an A".

And just for completeness there is also an ItemsPanelTemplate class that is used to change the internal layout of a list box - Charles Petzold's WPF book has an example of using it to make a normal list box display it's items in a circle.


An ItemTemplate describes how to render the data item in a listbox (for example, you might have your listbox bound to a List - the ItemTemplate will allow you to specify how to render a Foo). An ItemTemplate is a DataTemplate.

A ControlTemplate describes how the ListBox itself should be rendered

Tags:

Wpf