How to add items to a ListView dynamically in WPF?


  • I want to realize a listview, in which each row represents a user. And the listview will have the following columns:

    Column 1: an image, the portraitof a user.
    Column 2: an image, whether to be appear depends on whether the user has a camera.
    Column 3: an image to declare the level of the user.
    Column 4: a string corresponds to the id of the user.
    Column 5: a button to change the level of the user.

    Then, in the corresponding csharp code, I want to add users dynamically, which means I don't know what to add till I receive some message from the Internet.

    I have referred to the samples in MSDN, but cannot find any sample that can add items dynamically. Can anybody help me with this problem? If there are some samples, that would be better.
    Friday, June 19, 2009 6:33 AM


  • Hi,

    Think you can create an entity to hold the user details.Then you can bind the list view columns to each of the property.
    For the button you will have to change the data template.

    After creating the object dynamically just add it to the listview items.

    See this link for a sample

    Hope it helps
    FEAR NOT TO BE JUST Please mark posts as answers/helpful if it answers your query
    • Edited by Rahul P Nath Friday, June 19, 2009 7:16 AM Added the link
    • Marked as answer by Jim Zhou - MSFT Thursday, June 25, 2009 11:38 AM
    Friday, June 19, 2009 7:12 AM