WPF ListView ItemTemplate Tutorial

WPF  wpf 

WPF provides various templates for customization of controls. WPF ListView ItemTemplate is in one of these. ItemTemplate of ListView is useful when we have to change the visual presentation of bound data objects.

If your WPF ListView control is bound to collection of objects using ItemsSource proeprty and you have not set the ItemTemplate property, then it will call the ToString() method of each bound object and whatever is returned by the method it will display in the ListView.

If you want to customize the display of items, then you must set the DataTemplate by using ItemTemplate property.

Below is the simple example, in which I set the DataTemplate using ItemTemplate property of WPF ListView.

<Window x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="350" Width="525">
        <ListView x:Name="MyListView" ItemsSource="{Binding Employees}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock  Text="{Binding Id}" />
                        <TextBlock  Text="{Binding Name}" Margin="5,0,0,0" />
                        <TextBlock  Text="{Binding Age}" Margin="5,0,0,0" />
namespace WpfApplication1
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        private List<Employee> employees;

        public MainWindow()
            employees = new List<Employee>();
            employees.Add(new Employee { Id = 1, Name = "Kapil Malhotra", Age = 30 });
            employees.Add(new Employee { Id = 2, Name = "Raj Kundra", Age = 34 });
            employees.Add(new Employee { Id = 3, Name = "Amitabh Bachan", Age = 80 });
            employees.Add(new Employee { Id = 4, Name = "Deepak Khanna", Age = 72 });

            this.DataContext = this;

        public List<Employee> Employees
                return employees;

    public class Employee
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }

ListView Example

Join our newsletter and get an occasional email with a technology and DotNetPattern.com news update.