Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8670137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:38:34+00:00 2026-06-12T18:38:34+00:00

I’m trying to implement a search function in a custom ListView and as such

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:38:35+00:00Added an answer on June 12, 2026 at 6:38 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to select an H1 element which is the second-child in its group
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.