如何返回ListViewItems的集合

本文关键字:ListViewItems 集合 返回 何返回 | 更新日期: 2023-09-27 18:02:50

抱歉关于ListViewItems的新手问题,但以前从未使用过ListView,我想做的是将所有ListViewItemsAnimalManager类返回到UI lsbOverview控件。这种方法的原因是,我有一个Manager类处理所有的数据,并将其返回到UI控件。问题是我如何将所有项目返回lsboverView ListView ?

    // declared in AnimalManager class
    private static List<Animal> AnimalList { get; set; }

     public static ListViewItem DisplayAllAnimals()
    {
        //Show animals on ListView by proper column
        foreach (var animal in AnimalList)
        {
            ListViewItem item = new ListViewItem(animal.Id); // generated ID
            item.SubItems.Add(animal.AnimalSort); // AnimalSort
            item.SubItems.Add(animal.Name); //Name
            item.SubItems.Add(animal.Age); //Age
            item.SubItems.Add(animal.Gender.ToString()); // Animal gender
            return item;
        }
        // how to return all items back to UI ?
    }

    // Mainform UI class where its used
    lsbOverview.Items.Add(AnimalManager.DisplayAllAnimals());

如何返回ListViewItems的集合

你在找这个吗?

// declared in AnimalManager class
    private static List<Animal> AnimalList { get; set; }

     public static ListViewItem[] DisplayAllAnimals()
    {
        //Show animals on ListView by proper column
        var listViewItems = new List<ListViewItem>();
        foreach (var animal in AnimalList)
        {
            ListViewItem item = new ListViewItem(animal.Id); // generated ID
            item.SubItems.Add(animal.AnimalSort); // AnimalSort
            item.SubItems.Add(animal.Name); //Name
            item.SubItems.Add(animal.Age); //Age
            item.SubItems.Add(animal.Gender.ToString()); // Animal gender
            listViewItems.Add(item);
        }
        return listViewItems.ToArray();
    }

    // Mainform UI class where its used
    lsbOverview.Items.AddRange(AnimalManager.DisplayAllAnimals());

我认为建立一个动物数组,然后在listview上使用AddRange可以做到这一点:

public static List<ListViewItem> DisplayAllAnimals()
{
    var animals = new List<ListViewItem>();
    //Show animals on ListView by proper column
    foreach (var animal in AnimalList)
    {
        ListViewItem item = new ListViewItem(animal.Id); // generated ID
        item.SubItems.Add(animal.AnimalSort); // AnimalSort
        item.SubItems.Add(animal.Name); //Name
        item.SubItems.Add(animal.Age); //Age
        item.SubItems.Add(animal.Gender.ToString()); // Animal gender
        animals.Add(item);
    }
    return animals;
}

lsbOverview.Items.AddRange(AnimalManager.DisplayAllAnimals().ToArray());

我想你想要的是:

    // declared in AnimalManager class
    private static List<Animal> AnimalList { get; set; }

     public static IEnumerable<ListViewItem> DisplayAllAnimals()
    {
        //Show animals on ListView by proper column
        foreach (var animal in AnimalList)
        {
            ListViewItem item = new ListViewItem(animal.Id); // generated ID
            item.SubItems.Add(animal.AnimalSort); // AnimalSort
            item.SubItems.Add(animal.Name); //Name
            item.SubItems.Add(animal.Age); //Age
            item.SubItems.Add(animal.Gender.ToString()); // Animal gender
            yield return item;
        }
    }

    // Mainform UI class where its used
    lsbOverview.Items.AddRange(AnimalManager.DisplayAllAnimals().ToArray());

编辑:注意AddRange调用而不是Add。关于IEnumerable<>和yield的更多信息,我建议谷歌或搜索这里以获得更深入的了解。

您可以使用yield return返回IEnumerable<ListViewItem>

public static IEnumerable< ListViewItem> DisplayAllAnimals()
{
    //Show animals on ListView by proper column
    foreach (var animal in AnimalList)
    {
        ListViewItem item = new ListViewItem(animal.Id); // generated ID
        item.SubItems.Add(animal.AnimalSort); // AnimalSort
        item.SubItems.Add(animal.Name); //Name
        item.SubItems.Add(animal.Age); //Age
        item.SubItems.Add(animal.Gender.ToString()); // Animal gender
        yield return item;
    }

}

你可以这样使用:

lsbOverview.Items.AddRange(AnimalManager.DisplayAllAnimals().ToArray());