I've stumbled upon the following problem: I'm using the answer to Filter items in a ListView in real time to create a filtered list of items in LargeIcon view. I define the groups for the listview:
// 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);
}
I add items iteratively to the listview and the master list in a method with the following code:
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);
This is the code that handles the filtering when I type a letter in the 'filter' textbox:
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();
The problem occurs after I type the second letter of the string. It appears that the line:
channellistView.Items.Clear();
somehow alters the Groups collection in the master list. I know this because I set a breakpoint on the line above and display the master list group for a particular item. After executing the line above, the item's group is set to null. That results in the list now showing a "Default" grouping with the item whose group was nullified.
It's my understanding that the line in question should not be modifying the Group collection in any way.
CodePudding user response:
With a little bit more debugging, I was able to resolve this. I had noticed that the Group property also has an "Items" collection that tracks the items that are assigned to the group. During debugging, I noticed that the items tended to duplicate. This led me to check the code I was using to assign items. That's where I found the problem.
I was not creating a new instance of the items while adding them to the master list. I was using a copy of each item. So, I changed that code to:
ListViewItem mlvi = new ListViewItem();
mlvi.Text = Obj.title;
mlvi.ImageIndex = 1;
mlvi.Group = channellistView.Groups[CategoryName];
mlvi.Tag = Obj;
masterChannelList.Add(mlvi);
Additionally, I needed to change the code that filtered the results to:
// This filters and adds your filtered items to listView
foreach (ListViewItem item in masterChannelList.Where(lvi => lvi.Text.ToLower().StartsWith(searchmetroTextBox.Text.ToLower().Trim())))
{
ListViewItem filteredItem = new ListViewItem();
filteredItem.Text = item.Text;
filteredItem.Group = item.Group;
filteredItem.ImageIndex = item.ImageIndex;
channellistView.Items.Add(filteredItem);
}
The new code ensured that I was getting a new instance of the listview items and not a copy.