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

Deadly 提交于 2020-01-06 14:38:13

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6855833/whats-the-quickest-and-easiest-way-to-add-items-in-a-listview-that-has-multiple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!