Elmo
Elmo

Reputation: 6471

What's the quickest and easiest way to add items in a ListView that has multiple columns?

I have a ListView called lv with three columns. What's the quickest and easiest way to add items in it during runtime? I am using WPF.

Upvotes: 0

Views: 206

Answers (1)

Klikuji
Klikuji

Reputation: 26

Try this:

<ListView 
        x:Name="lv" 
        ItemsSource="{Binding Path=Items}"
        SelectedItem="{Binding Path=SelectedItem}">

        <ListView.View>
        <GridView >
            <GridViewColumn Header="Header1" DisplayMemberBinding="{Binding Path=Prop1}" />
            <GridViewColumn Header="Header2" DisplayMemberBinding="{Binding Path=Prop2}"/>
            <GridViewColumn Header="Header3" DisplayMemberBinding="{Binding Path=Prop3}"/>
            </GridView>
        </ListView.View>

</ListView>

In your ViewModel you should have some collection, like this:

  public ObservableCollection<Test> Items { get; protected set; }

where Test is :

public class Test
{
    public int Prop1{ get; set; }
    public String Prop2{ get; set; }
    public int Prop3{ get; set; }
}

When you will put/remove data in this "Items" Property, ListView will update itself automaticly.

Upvotes: 1

Related Questions