winforms 为什么ListView拒绝显示它的列、项和子项(只显示Group)?

zbq4xfa0  于 6个月前  发布在  其他
关注(0)|答案(5)|浏览(55)

ListView看起来像一只臭鼬一样脾气暴躁,像一个[被P.C.警察省略]
在-和其他人的帮助下,我能够让ListView按照我想要的那样工作(http://stackoverflow.com/questions/11423537/how-to-add-subitems-to-a-listview)。
现在,我正在将这个简单的演示转换为真实的应用程序。
我在表单的Load事件中创建列:

listViewGroupsItems.Columns.Add(colHeadName);
listViewGroupsItems.Columns.Add(colHeadRate);

字符串
.但它们不显示。然而,在我的第一个项目上方有一个空行。那么为什么没有显示文本值。它们“存在”但不可见吗?为什么它们会被清除?
不管怎样,我想看到的是:

column1Title    column2Title
Group1Name
Item        subitem
Item        subitem
Group2Name
Item        subitem
Group1Name
Item        subitem
Item        subitem
Item        subitem
Item        subitem


……但我真正看到的是:

[blank line]
Group1Name


就是这样
ListView的View属性设置为Details;否则,所有属性都为默认值。
我的listview代码是:

private void AddGroupsAndItems() {
    Dictionary<string, string> GroupsDict = PlatypusData.GetGroupsForTopLevel(Convert.ToInt32(labelTopLevel.Tag));
    int currentGroup = 0;
    foreach (KeyValuePair<string, string> entry in GroupsDict) {
        string GroupNumber = entry.Key;
        string GroupName = entry.Value;
        listViewGroupsItems.Groups.Add(new ListViewGroup(GroupName, HorizontalAlignment.Left));

        Dictionary<string, string> ItemsDict = PlatypusData.GetItemsForGroup(GroupNumber);
        foreach (KeyValuePair<string, string> itemEntry in ItemsDict) {
            string itemID = itemEntry.Key;
            string itemName = itemEntry.Value;
            ListViewItem lvi = new ListViewItem(string.Format("{0} ({1})", itemName, itemID));
            lvi.SubItems.Add(PlatypusData.GetDuckbillNameForItemId(itemID));
            listViewGroupsItems.Items.Add(lvi);
            listViewGroupsItems.Groups[currentGroup].Items.Add(lvi);
        }
        currentGroup++;
    }
}

更新

我将窗体的Load事件中的代码从:

var colHeadName = new ColumnHeader { Text = Resources.RateRequestForm_RateRequestForm_Load_Billing_Account_Client, Width = 160 };
var colHeadRate = new ColumnHeader { Text = Resources.RateRequestForm_RateRequestForm_Load_RatePlan_ID, Width = 120 };
listViewCustomerBillAccountsClients.Columns.Add(colheadName); //colHeadName);
listViewCustomerBillAccountsClients.Columns.Add(colheadRate); //colHeadRate);


.致:

ColumnHeader colheadName = new ColumnHeader();
ColumnHeader colheadRate = new ColumnHeader();
listViewCustomerBillAccountsClients.Columns.Add(colheadName); 
listViewCustomerBillAccountsClients.Columns.Add(colheadRate);


......这根本没什么区别。
看起来,ColumnHeader构造函数应该能够接受一个它应该显示的字符串,但即使我这样做:

ColumnHeader colheadName = new ColumnHeader("This");
ColumnHeader colheadRate = new ColumnHeader("That");


..

e37o9pze

e37o9pze1#

迟到了,但我刚刚花了一些时间试图解决一个类似的问题。我发现的答案是,当清除列表之前填写,你必须使用

listviewGroupItems.Items.Clear();

字符串

listviewGroupItems.Clear();


ListView.Clear()方法清除控件中的所有内容--包括列

4sup72z8

4sup72z82#

我今天遇到了同样的问题。我已经编写了user 2867342提到的listView.Clear()。我需要将其更改为listView.Items.Clear(),但这并没有使列出现。列在那里,我可以单击它们并调整它们的大小,但它们完全是空白的。
我有一个ListView设置为细节模式。我还设置了OwnerDraw property为true,因为我想画我自己的进度栏列。MSDN说下面关于OwnerDraw属性(强调我的):
ListView控件通常由操作系统绘制。为了自定义ListView项、子项和列标题的外观,请将OwnerDraw属性设置为true,并为以下一个或多个事件提供处理程序:DrawItem、DrawSubItem和DrawColumnHeader。这称为所有者绘制。当View属性设置为View.Details时,所有三个事件都会发生;否则,只发生DrawItem事件。
我必须实现DrawColumnHeader事件。在我的情况下,defualt工作正常,因此该方法将DrawDefault事件参数设置为true。实现此事件处理程序后,列标题正确显示:

...Windows.Forms designer code...
listView.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(this.listView_DrawColumnHeader);
...

private void listView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    e.DrawDefault = true;
}

字符串

1zmg4dgp

1zmg4dgp3#

代码中缺少列标题。(已修复)
根据MSDN:
如果ListView控件未指定任何列标题,并且您将View属性设置为View.Details,则ListView控件将不显示任何项。如果ListView控件未指定任何列标题,并且您将View属性设置为View.Tile,则ListView控件将不显示任何子项。
当然,你可能需要做更多的调整比你看到我的SS,但它至少回答了你的问题,为什么你得到空白。


的数据
编辑(更改了行,尽管将它们更改回您的代码并没有影响我的成功结果):

lvi.Group = listViewGroupsItems.Groups[currentGroup];
listViewGroupsItems.Items.Add(lvi);

字符串

xkrw2x1b

xkrw2x1b4#

还有一件事要检查,我刚刚发现:OwnerDraw必须为false。
从以前的项目复制和粘贴,忘记了我使用OwnerDraw

des4xlb0

des4xlb05#

我遇到了同样的问题。我的解决方案是View.Details。

listView.View = View.Details;
listView.Columns.Add("Column 1");
listView.Columns.Add("Column 2");

listView.Items.Add("xxx");
listView.Items[index].SubItems.Add("xxx");

字符串

相关问题