3

很抱歉关于新手的问题,ListViewItems但以前从未使用ListView过,我想做的是将所有内容ListViewItemsAnimalManager类返回到 UIlsbOverview控件。这种方法的原因是我有一个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());
4

4 回答 4

2

你在找这个吗?

// 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());
于 2013-03-08T10:40:26.053 回答
1

我想你想要的是这样的:

    // 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 的更多信息,我建议谷歌或在此处搜索以获得更深入的了解。

于 2013-03-08T10:38:33.370 回答
1

我认为构建一组动物,然后在列表视图上使用 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());
于 2013-03-08T10:44:53.913 回答
0

您可以使用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());
于 2013-03-08T10:40:36.723 回答