我偶然發現了以下問題:我正在使用實時過濾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 集合。
uj5u.com熱心網友回復:
通過更多的除錯,我能夠解決這個問題。我注意到 Group 屬性還有一個“Items”集合,用于跟蹤分配給該組的專案。在除錯程序中,我注意到這些專案往往會重復。這讓我檢查了我用來分配專案的代碼。這就是我發現問題的地方。
在將它們添加到主串列時,我沒有創建專案的新實體。我正在使用每個專案的副本。所以,我將該代碼更改為:
ListViewItem mlvi = new ListViewItem();
mlvi.Text = Obj.title;
mlvi.ImageIndex = 1;
mlvi.Group = channellistView.Groups[CategoryName];
mlvi.Tag = Obj;
masterChannelList.Add(mlvi);
此外,我需要將過濾結果的代碼更改為:
// 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);
}
新代碼確保我獲得的是串列視圖項的新實體,而不是副本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/371333.html
下一篇:我想將我的串列視圖放在下拉串列中
