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

  • Home
  • SEARCH
  • 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 176133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:49:21+00:00 2026-05-11T13:49:21+00:00

First the code (sorry if its not 100%) I am no expert and then

  • 0

First the code (sorry if its not 100%) I am no expert and then the question follows.

    public partial class Window1 : Window     {     CollectionView cv;      public Window1()     {         InitializeComponent();          List<Person> ppl = new List<Person>();         BitmapImage b = new BitmapImage(new Uri(@'http://i.stackoverflow.com/Content/Img/stackoverflow-logo-250.png',UriKind.Absolute));         ppl.Add(new Person(b, 'world1'));         ppl.Add(new Person(b, 'world2'));         ppl.Add(new Person(b, 'world3'));         ppl.Add(new Person(b, 'world4'));         ppl.Add(new Person(b, 'world5'));         ppl.Add(new Person(b, 'world6'));          lb.ItemsSource = ppl;         lb.SelectedIndex = 1;          cv = (CollectionView)CollectionViewSource.GetDefaultView(lb.ItemsSource);          new TextSearchFilter(cv, textBox1);     } }  public class TextSearchFilter {     public TextSearchFilter(CollectionView cv, TextBox tb)     {         string filterText = '';          cv.Filter = delegate(object obj)         {             Person p = obj as Person;             int index = p.Info.IndexOf(filterText,0,StringComparison.InvariantCultureIgnoreCase);             return index > -1;         };          tb.TextChanged += delegate         {             filterText = tb.Text;             cv.Refresh();         };     } }      class Person     {         private BitmapImage myImage;         private string myInfo = '';          public BitmapImage Image         {             get { return myImage; }             set { myImage = value; }         }          public string Info         {             get { return myInfo; }             set { myInfo = value; }         }          public Person(BitmapImage Image, string Info)         {             this.Image = Image;             this.Info = Info;         }     } 

Thank you for reading so far, as you would have understood by now is that the code filters a listbox based on the input in the textbox, which works like a charm btw.

My problem is how do I preserve the selection during filtering. When the window loads, the listbox has all the items in it and I select the first item, then I type something in the textbox and the listbox filters to only show the relevant items, after selecting another item I remove all the text from the text box which brings it back to the original state but this time the selection has changed to only the item that I selected in the filtered view (so instead of two items being shown as selected only one shows as selected). This behaviour is obvious as I am filtering on a collection, so the moment the collection changes the selection is lost.

Is there a way to preserve the selection? any pointers?

Many Thanks.

  • 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. 2026-05-11T13:49:21+00:00Added an answer on May 11, 2026 at 1:49 pm

    Probably in your TextChanged event handler, you should store the ListBox’s selected items, like so:

    Person[] selectedPeople = new Person[lb.SelectedItems.Count]; lb.SelectedItems.CopyTo(selectedPeople, 0); 

    Then after refreshing the filter, add them back using:

    foreach (Person p in selectedPeople) {     if(lb.Items.Contains(p))     {         lb.SelectedItems.Add(p);     } } 

    The reason I have it using CopyTo and saving in an array instead of just using:

    IList selectedPeople = lb.SelectedItems; 

    is that the latter will just keep a reference to lb.SelectedItems, and the foreach would modify the enumeration, causing an exception to be thrown. There may be an easier way, but this should get you headed in the right direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 91k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To "call home" you would need a Adobe LiveCycle Policy… May 11, 2026 at 6:17 pm
  • Editorial Team
    Editorial Team added an answer You could use .net through COM Interop, and do your… May 11, 2026 at 6:17 pm
  • Editorial Team
    Editorial Team added an answer Why don't you just get the first item with a… May 11, 2026 at 6:17 pm

Related Questions

I've been arguing with my coworkers about Pascal casing (upper camel case) vs. lower
How do you gracefully handle failed future feature imports? If a user is running
I have this piece of code: var myObj = function () { this.complex =
Sorry, i had to put the same post again due to still no solution

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.