31

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!

2 Answers 2

38

It works changing the code to:

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

Now it looks obvious but ... go figure!

Sign up to request clarification or add additional context in comments.

2 Comments

Do you also happen to know how to solve the same issue, having a general list, that can take different objects, with different properties?
WOw another Persie fan here OR Persie can code! :) anyways, thx @RobinVanPersi
1
List<MyData> yourList= new List<MyData>();
//adding elements to yourList (...)


foreach (MyData m in yourList)
{
    myListView.Items.Add(m);
}

1 Comment

Who voted this down, rather: why??? This seems a useful answer. I'd like to learn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.