5 minutes Guide of WPF ListView ItemTemplate

ListView control in WPF is used for display a list of items. Every list view item has its own UI for display its own data. But how every listview item is rendered, it's depend on the ItemTemplate property of ListView.

ItemTemplate property is used for get and set the DataTemplate of every list view item. DataTemplate defines how each item appears.

You defines a single DataTemplate in the ItemTemplate property and it is used by all list view items automatically.

The main benefit of specify DataTemplate in Resources section is reusability. You can define DataTemplate in Resource section and use it with many ListView. You need to set the ItemTemplate property by using the StaticResource extension like in the above line 13. You can set DataTemplate as both StaticResource and DynamicResource extension.

Or you can find the DataTemplate in code behind and set ListView ItemTemplate property like below.

When WPF creates listview items at runtime using the ItemTemplate, it's copy the ItemTemplate property of the ListView to the ContentTemplate property of the each ListBoxItem. Then ContentTemplate is used by every ListViewItem for creating its UI.

You can also change the DataTemplate of each listview Item by using ContentTemplate property of every ListViewItem.

Below is the example of using two DataTemplates on alternate ListViewItems.