我偶然发现了以下问题:我正在使用实时过滤 ListView 中的项目的答案在 LargeIcon 视图中创建过滤后的项目列表。我为列表视图定义了组:
// Define the Groups within the listview.
foreach (CategoryObject category in JManager.jfo.categories)
{
ListViewGroup lvg = new ListViewGroup();
lvg.Header = lvg.Name = category.name;
channellistView.Groups.Add(lvg);
}
我使用以下代码将项目迭代地添加到列表视图和主列表中:
lvi.Group = channellistView.Groups[CategoryName];
lvi.Tag = Obj;
channellistView.Items.Add(lvi);
ListViewItem mlvi = lvi.Clone() as ListViewItem;
mlvi.Group = channellistView.Groups[CategoryName];
masterChannelList.Add(mlvi);
这是当我在“过滤器”文本框中键入字母时处理过滤的代码:
channellistView.BeginUpdate();
channellistView.Items.Clear();
// This filters and adds your filtered items to listView
foreach (ListViewItem item in masterChannelList.Where(lvi =>
lvi.Text.ToLower().StartsWith(searchmetroTextBox.Text.ToLower().Trim())))
{
channellistView.Items.Add(item);
}
channellistView.EndUpdate();
在我键入字符串的第二个字母后出现问题。看来该行:
channellistView.Items.Clear();
以某种方式更改主列表中的组集合。我知道这一点是因为我在上面的行上设置了一个断点并显示特定项目的主列表组。执行上述行后,项目的组设置为空。这导致列表现在显示一个“默认”分组,其中包含其组被取消的项目。
我的理解是,有问题的行不应该以任何方式修改 Group 集合。