Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListView - how to add items programmatically?

Even if I know it's not ideal - I need to programmatically populate a listView (for whatever reason).

I am declaring my columns in the markup:

            <ListView.View>                 <GridView>                     <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}"/>                     <GridViewColumn Header="Value" DisplayMemberBinding="{Binding Path=Value}"/>                 </GridView>             </ListView.View> 

I am adding the items like this in code (it's obviously in a loop):

            MyData data = getDataItem(index); //< -- whatever             ListViewItem item = new ListViewItem();             item.DataContext = data;             this.myListView.Items.Add(item); 

Where MyData is defined as:

public class MyData {     public string Name { get; set; }     public string Value { get; set; } } 

The items are being added (I can see the rows) but I don't see any content.

Anyone any clue?

Any help appreciated!

like image 728
JohnIdol Avatar asked Aug 20 '09 11:08

JohnIdol


People also ask

How to add items in ListView WPF?

On button click event handler, we add the contents of TextBox to the ListView by calling ListView. Items. Add method. Now if you enter text in the TextBox and click Add Item button, it will add contents of the TextBox to the ListView.

What is List view in WPF?

The ListView control provides the infrastructure to display a set of data items in different layouts or views. For example, a user may want to display data items in a table and also to sort its columns. The types referenced in this article are available in the Code reference section.


1 Answers

It works changing the code to:

        MyData data = getDataItem(index); //< -- whatever         this.myListView.Items.Add(data); 

Now it looks obvious but ... go figure!

like image 138
JohnIdol Avatar answered Sep 22 '22 13:09

JohnIdol