我正在尝试在自定义中实现搜索功能,ListView因此我隐藏Items了一个ObservableCollection允许的自定义AddRange,类似于damonpayne.com 上定义的那个(对于 tl;dr-ers 基本上它OnCollectionChanged在添加时抑制触发事件然后使用 ) 触发多个项目NotifyCollectionChangedAction.Reset:
public new MyCollection<ListViewItem> Items { get; protected set; }
MyCollection_CollectionChanged()人口base.Items:_
this.BeginUpdate();
base.Items.Clear();
base.Items.AddRange(this.Items.ToArray());
this.EndUpdate();
这个想法是,当项目不满足搜索条件时,它们会从base.Items(即System.Windows.Forms.ListView)中删除,但仍保留在this.Items(即My.Name.Space.MyListView)中。当搜索被取消或条款改变时,base.Items可以通过 重新填充this.Items。
除了一个小但重要的警告外,这可以正常工作并且符合预期:
问题是ListViewItems'Group并非始终如一地从this.Itemsto携带base.Items,因此所有项目都出现在“Default”组中。
关于为什么会发生这种情况以及如何解决它的任何想法?
更新
我仍然坚持这一点。当然这样做.ToArray()只是创建了一个浅表副本,Items所以Group应该保留?Maverik证实了这一点:
更新 2
好的,经过更多调查,我发现它发生在哪里。
将ListViewItems 添加到 时MyCollection<ListViewItem>:
var item0 = new ListViewItem();
var item0.Group = this.Groups["foo"];
//here this.Items.Count = 0
this.Items.Add(item0);
//here this.Items.Count = 1 with item0 having group "foo"
var item1 = new ListViewItem();
var item1.Group = this.Groups["bar"];
//here this.Items.Count = 1 with item0 having group "foo"
this.Items.Add(item1);
//here this.Items.Count = 2 with item0 having group "null" and item1 having group "bar"
我还检查了这个替换MyCollection<为正常ObservableCollection<的,同样的情况仍然发生。
更新 3 - 解决方案
请看我的回答。