C# ListView 隐藏或折叠所选组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12445504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:18:45  来源:igfitidea点击:

ListView hide or collapse selected group

c#winformslistview.net-3.5show-hide

提问by sczdavos

How can I hide or collapse some group in ListView?

如何隐藏或折叠某些组ListView

I just add some items

我只是添加了一些项目

contactListView.Items.Add(new ISIMlistViewItem(contact));
if (contact.availability == 6)
    contactListView.Items[contact.identificator].Group = contactListView.Groups["offlineGroup"];
else
    contactListView.Items[contact.identificator].Group = contactListView.Groups["onlineGroup"];

And I want to sometimes hide the offlineGroup.

我有时想隐藏offlineGroup.

if (hideOffline == true)
{
    // something like
    contactListView.Groups["offlineGroup"].Hide();
    // or
    contactListView.Groups["offlineGroup"].Visible = false;
}

But I don't know how can I do that. Can I just collapse it and don't draw it or is there any possibility to hide it?

但我不知道我该怎么做。我可以折叠它而不绘制它还是有可能隐藏它?

回答by Uwe Keim

It seems that the .NET version of the ListViewGroupclassdoes not provide a Collapseor Expandmethod.

似乎ListViewGroup该类的 .NET 版本没有提供CollapseorExpand方法。

Luckily, the native ListViewcontrol does support it and one guy provided an extension to enable expand and collapse.

幸运的是,本机ListView控件确实支持它,并且一个人提供了一个扩展来启用 expand 和 collapse

Using his code you can then have a function to set the expand/collapse state with:

使用他的代码,您可以使用以下函数来设置展开/折叠状态:

private void SetGroupCollapse(GroupState state)

For hiding a complete group I would simply remove all the items in this group.

为了隐藏一个完整的组,我只需删除该组中的所有项目。