ItemTemplate vs ControlTemplate

I am confused about learning WPF. I see that the ControlTemplate is used to determine what each item in the list looks like. Isn't that what ItemTemplate is used in lists? What is the difference?

+5
wpf
Feb 22 '09 at 18:25
source share
2 answers

The ControlTemplate allows you to change the appearance of existing controls, for example, to make a regular everyday button look like shiny glass buttons on the Vista taskbar or to 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 appearance - so the button is "what you click" and not "a rectangle with some kind of three-dimensional effect that you can click on."

You can replace the ListBox control template to get something like a rounded list or custom scrollbars.

A DataTemplate (ItemTemplate is a property of type DataTemplate), on the other hand, allows you to control how the data rows displayed in lists (and other similar controls) in data templates are used to indicate things such as "Show last name than comma and the first and last name must be in bold and the name must be green if it starts with a letter. "

And just for completeness, there is also the ItemsPanelTemplate class, which is used to change the internal layout of the list - in the book of Charles Petzold WPF there is an example of its use so that the regular list displays elements in a circle.

+11
Feb 23 '09 at 8:45
source share

An ItemTemplate describes how to visualize a data item in a list (for example, you can attach your list to a list - ItemTemplate allows you to specify how to visualize Foo). ItemTemplate is a DataTemplate.

The ControlTemplate element describes how the ListBox itself should be displayed.

+3
Feb 22 '09 at 18:35
source share



All Articles