break

WPF: ListViewItem in a ListView

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.
listView

5 Responses

  1. Andreas Says:

    can you tell me, how to add a ListView item in two columns using c# code?
    Thanks…

  2. Greg Says:

    i also have to same question of the last comment.

    Thx
    Greg

  3. Fernando Says:

    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!

  4. Fernando Says:

    <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>

  5. Willem Says:

    WPF: ListViewItem in a ListView

    Nice example, but how do you add items programmatically if you have different columns (GridView)?

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

CAPTCHA Image
Reload Image