Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListViewItem's group not being preserved through another collection

I'm trying to implement a search function in a custom ListView and as such I am hiding Items with a custom ObservableCollection which allows AddRange, similar to the one defined on damonpayne.com (for the tl;dr-ers out there basically it suppresses firing OnCollectionChanged event while adding multiple items then fires with NotifyCollectionChangedAction.Reset):

public new MyCollection<ListViewItem> Items { get; protected set; }

The MyCollection_CollectionChanged() populates base.Items:

this.BeginUpdate();
base.Items.Clear();
base.Items.AddRange(this.Items.ToArray());
this.EndUpdate();

The idea is that when items do not fulfill the search terms, they are removed from base.Items (i.e. System.Windows.Forms.ListView) but remain in this.Items (i.e. My.Name.Space.MyListView). When the search is cancelled or the terms change, base.Items can be repopulated by this.Items.

This works fine and as expected except for one small but important caveat:

The problem is that ListViewItems' Group is not consistently being carried from this.Items to base.Items and as such all the items appear in the group "Default".

Any ideas as to why this is happening and how to fix it?

Update

I'm still stuck on this. Surely doing the .ToArray() just creates a shallow copy of the Items so the Group should be preserved? This has been confirmed by Maverik:

I just called in Linqpad, and while the list reference is different, you're right.. object references are same.

Update 2

Okay after some more investigating I've found where it is happening.

When adding ListViewItems to the 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"

I also checked this replacing MyCollection< with the normal ObservableCollection< and the same still occurs.

Update 3 - Solution

Please see my answer.

like image 836
dav_i Avatar asked Sep 18 '12 12:09

dav_i


1 Answers

Background

Figured it out!

I don't know why but an exception wasn't being thrown when it should have been.

The exception Cannot add or insert the item 'item0' in more than one place. should have been thrown.

(After clearing out some stuff from a .designer.cs, it was thrown (though curiously not when stepped through...)

As requested the code cleared from the designer of the form containing the custom ListView was

this.ListView.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
    listViewItem1,
    listViewItem2,
    listViewItem3,
    listViewItem4,
    listViewItem5,
    listViewItem6});

These were just some temporary items which I had added previously for test but the designer must have remembered them for some reason.)


The Solution

The solution is as described in this SO answer and my code now reads:

this.BeginUpdate();
base.Items.Clear();
base.Items.AddRange(this.Items.Select((ListViewItem)a => a.Clone()).ToArray());
this.EndUpdate();
like image 91
dav_i Avatar answered Sep 23 '22 15:09

dav_i