如果列表视图中的组中没有项目,则显示组标题?

时间:2013-11-04 15:29:04

标签: c# .net winforms

我想在列表视图(winform)中对项目进行分组,其中组标题显示组中的项目数(包括0项)。 我的问题是,当我从组或列表视图中清除项目时,组标题会消失。

如何确保群组标题不会消失?我看不到要在ListView或ListViewGroup类中设置的任何属性。

谢谢

考虑代码:

    ListViewGroup groupA;
    ListViewGroup groupB;

    private void button1_Click(object sender, EventArgs e)
    {
        //Create group A and B and add them to the groups collection
        groupA = new ListViewGroup("A");
        listView1.Groups.Add(groupA);
        groupB = new ListViewGroup("B");
        listView1.Groups.Add(groupB);

        //Add 2 items in the list view and update the group headers
        listView1.Items.Add(new ListViewItem("Item1", groupA));
        groupA.Header = "A - 1 item";

        listView1.Items.Add(new ListViewItem("Item2", groupB));
        groupB.Header = "B - 1 item";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //Clear the items
        listView1.Items.Clear();

        //Update header
        //The problem here is that the headers are not displayed any more
        groupA.Header = "A - 0 item";
        groupB.Header = "B - 0 item";
    }

1 个答案:

答案 0 :(得分:0)

我在回答我自己的问题!用普通的ListView似乎无法做到这一点。我也查看了底层的Win32消息但没有成功。最后,我放弃了,我买了第三方控件。