很抱歉有关ListViewItems
的新手问题,但之前从未使用过ListView
,我尝试做的是将所有ListViewItems
从AnimalManager
类返回给用户界面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());
答案 0 :(得分: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());
答案 1 :(得分: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&lt;&gt;的更多信息和产量我会建议谷歌或在这里搜索更深入的了解。
答案 2 :(得分:1)
我认为构建一系列动物,然后在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());
答案 3 :(得分: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());