A friend of mine asked me How to add an item to a ListView using a WPF ListViewItem.
First of all, you can add items to a <ListView> either in the XAML, or in the .cs file of your project using the following line:
listView1.Items.Add("Daniel");
But if for some reason you want to use a ListViewItem you can do something like this:
Window1.xaml
<Window x:Class=”WPFListView.Window1″
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”Window1″ Height=”300″ Width=”300″>
<Grid>
<ListView Margin=”65,85,107,86″ Name=”listView1″ />
</Grid>
</Window>
Window1.xaml.cs
public Window1()
{
InitializeComponent();
ListViewItem item;
List < string > items = new List< string >();
items.Add(”Hello”);
items.Add(”Daniel”);
foreach (string names in items)
{
item = new ListViewItem();
item.Content = names;
listView1.Items.Add(item);
}
}
The important part of this code, which is what my friend couldn’t find is that to add a text to a ListViewItem you use the Content property.

October 4th, 2007 at 10:50 am
can you tell me, how to add a ListView item in two columns using c# code?
Thanks…
January 14th, 2008 at 6:46 pm
i also have to same question of the last comment.
Thx
Greg
January 25th, 2008 at 5:13 pm
This is what I do when I have this problem:
XMAL:
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace ListViewExperiment
{
///
/// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
private ObservableCollection names = new ObservableCollection();
public Window1()
{
InitializeComponent();
listView.DataContext = names;
//here we add the items:
names.Add(new PersonName(”Fernando”, “Romero”));
names.Add(new PersonName(”Juan”, “Perez”));
names.Add(new PersonName(”Juan”, “De Los Palotes”));
names.Add(new PersonName(”Fulano”, “De Tal”));
}
}
public class PersonName
{
public PersonName(string firstName, string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
Basically, you are adding just one item and what you get is a display of the properties in different columns.
I hope this could be helpful. Good luck!
February 20th, 2008 at 4:09 pm
<Window x:Class=”BlogStuff.Window1″
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”My Simple Grid” Height=”300″ Width=”300″>
<ListView ItemsSource=”{Binding}”>
<ListView.View>
<GridView>
<GridViewColumn Header=”First Name” DisplayMemberBinding=”{Binding Path=FirstName}”>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header=”Last Name” DisplayMemberBinding=”{Binding Path=LastName}”>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Window>
February 11th, 2009 at 8:26 am
WPF: ListViewItem in a ListView
Nice example, but how do you add items programmatically if you have different columns (GridView)?