gpt4 book ai didi

c# - 如何将子项添加到 ListView?

转载 作者:太空狗 更新时间:2023-10-29 21:23:43 25 4
gpt4 key购买 nike

我正在尝试获取具有子项的 Listview 的最简单示例。但是这段代码:

private void button1_Click(object sender, EventArgs e) {
listView1.Groups.Add(new ListViewGroup("Kannst du mich sehen?", HorizontalAlignment.Left));
string[] strArr = new string[4] { "uno", "dos", "twa", "quad" };
for (int i = 0; i < strArr.Length; i++)
{
ListViewItem lvi = new ListViewItem(strArr[i]);
listView1.Items.Add(lvi);
lvi.SubItems.Add("Ciao, Baby!");
listView1.Items[i].Group = listView1.Groups[0];
}
}

...不显示子项(“Ciao, Baby!”)。它显示:

Kannst du mich sehen?
---------------------
uno dos twa quad

...但我希望它是:

Kannst du mich sehen?
---------------------
uno Ciao, Baby!
dos Ciao, Baby!
twa Ciao, Baby!
quad Ciao, Baby!

甚至更陌生(在我看来),我明白了:

Default
-------
uno dos twa quad uno dos twa quad

FIRST
-----
uno dos twa quad

...使用此代码:

private void button2_Click(object sender, EventArgs e) {
string[] strArrGroups = new string[3] { "FIRST", "SECOND", "THIRD" };
string[] strArrItems = new string[4] { "uno", "dos", "twa", "quad" };
for (int i = 0; i < strArrGroups.Length; i++)
{
listView1.Groups.Add(new ListViewGroup(strArrGroups[i], HorizontalAlignment.Left));
for (int j = 0; j < strArrItems.Length; j++) {
ListViewItem lvi = new ListViewItem(strArrItems[j]);
listView1.Items.Add(lvi);
lvi.SubItems.Add("Hasta la Vista, Mon Cherri!");
listView1.Items[j].Group = listView1.Groups[i];
}
}
}

更新

ListView 的 View 属性有点奇怪,如果你问我:

默认的“LargeIcon”设置与最初显示的一样(SmallIcon 具有相同的效果)。“Detail”只给我组标题(没有项目)“List”给我项目(一个在一行,如我所愿),但没有组/标题“Tile”给我:

Kannst du mich sehen?
---------------------
uno dos
twa quad

...使用 button1 的代码和:

Default
---------------------
uno dos
twa quad
uno dos
twa quad

FIRST
---------------------
uno dos
twa quad

...使用 button2 的代码

或者:

1) I'm stupid
2) ListView is very counterintuitive
-or
3) Nobody ever wants to use the ListView the way I'm trying to use it...if that's the case, should I be using a different control instead?

~~~侧面(或底部)问题:由于我住在 StackOverflow,是否有可能成为双重公民,这样做有任何税收优惠吗?

最佳答案

这对我有用:

listView1.Columns.Add("Col1");
listView1.Columns.Add("Col2");

string[] strArrGroups = new string[3] { "FIRST", "SECOND", "THIRD" };
string[] strArrItems = new string[4] { "uno", "dos", "twa", "quad" };
for (int i = 0; i < strArrGroups.Length; i++)
{
int groupIndex = listView1.Groups.Add(new ListViewGroup(strArrGroups[i], HorizontalAlignment.Left));
for (int j = 0; j < strArrItems.Length; j++)
{
ListViewItem lvi = new ListViewItem(strArrItems[j]);
lvi.SubItems.Add("Hasta la Vista, Mon Cherri!");
listView1.Items.Add(lvi);
listView1.Groups[i].Items.Add(lvi);
}
}

事实证明,您必须将项目添加到组中,而不是在项目上设置组属性,如其他问题所示。非常非常奇怪。

结果是:

enter image description here

在 .Net 4、WinForms、VS2010 中测试

关于c# - 如何将子项添加到 ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11423537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com