C#设置ListView的项的子项的文本不显示 [英] C# setting ListView's Item's Subitem's text does not display

查看:812
本文介绍了C#设置ListView的项的子项的文本不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有些失落. 当我最初构建ListView时,它完全可以按预期工作:

I am losing my mind a little bit here. When I initially build up a ListView, it works totally as expected:

ListViewItem listViewItem = listView.Items.Add(model.Id.ToString(), model.Name, model.Id.ToString());
listViewItem.SubItems.AddRange(new ListViewItem.ListViewSubItem[]
  {
    new ListViewItem.ListViewSubItem() {Name = "ArtNr", Text = model.ArticleNumber},
    new ListViewItem.ListViewSubItem() {Name = "Quantity", Text = "XXX"},
  });

我在控件中定义了3列,并且分别显示了Name,ArticleNumber和"XXX".

I have 3 columns defined in the control, and they display, respecively, Name, ArticleNumber and "XXX", to begin with.

稍后,我必须用实际数据替换"XXX",然后执行以下操作:

At a later stage, I have to replace "XXX" with actual data, and do the following:

foreach (ListViewItem listViewItem in listView.Items)
  {
    InvoiceLine invoiceLine = FindInvoiceLine(...);
    if (invoiceLine == null)
        continue;

    listViewItem.SubItems["Quantity"].Text = invoiceLine.Quantity.ToString();
  }

我可以看到Text实际上是在正确的SubItem.Text中设置的,也可以稍后查看,同时通过其他引用引用ListView,即我知道自己正在查看正确的对象. 但是,GUI并未反映出更改,它在第三列中仍显示"XXX".

I can see the Text actually being set in the proper SubItem.Text, also if I check later, while referencing the listview through a different reference, i.e. I know I am looking at the right object. However GUI does not reflect the change, it still says "XXX" in the 3rd column.

有什么用?!

推荐答案

在挖掘源代码.NET Framework之后,我发现它确实是 AddRange 方法的错误.他们忘记设置ListViewSubItem的所有者,因此在设置Text属性时不会更新文本.

After digging source code .NET Framework, I found that it's definitively bug of AddRange method. They forgot to set owner of ListViewSubItem, so the text was not update when setting Text property.

您应该改用 Add(ListViewSubItem).

或使用带有参数的子项构造函数为其设置所有者.

or use subitem constructor with parameter to set owner for it.

listViewItem.SubItems.AddRange(new ListViewItem.ListViewSubItem[]
  {
    new ListViewItem.ListViewSubItem(listViewItem, model.ArticleNumber) {Name = "ArtNr" },
    new ListViewItem.ListViewSubItem(listViewItem, "XXX") {Name = "Quantity" },
  });

这篇关于C#设置ListView的项的子项的文本不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆