Removing many items at once

4 posts, 0 answers
  1. Kristian
    Kristian avatar
    4 posts
    Member since:
    Feb 2013

    Posted 04 Mar 2014 Link to this post

    In my app I have an ObservableCollection with 1000 items, and sometimes the user needs to remove 100 items at once.
    The problem I have is that removing items one by one by calling the RemoveAt method takes too long to complete and the UI thread blocks for 3-5 seconds.
    Is there a way to stop updating the view, and then after removing the items enable it again and redraw the list?
  2. Deyan
    Admin
    Deyan avatar
    2039 posts

    Posted 07 Mar 2014 Link to this post

    Hello Kristian,

    Can you try setting the ItemsSource to null, remove the items from your source, and again bind it to the listbox?

    Regards,
    Deyan
    Telerik

    DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

  3. DevCraft banner
  4. Kristian
    Kristian avatar
    4 posts
    Member since:
    Feb 2013

    Posted 07 Mar 2014 in reply to Deyan Link to this post

    But this resets the vertical offset of the scrollviewer, and I need the ListBox to not change vertical offset if possible.
  5. Deyan
    Admin
    Deyan avatar
    2039 posts

    Posted 07 Mar 2014 Link to this post

    Hi Kristian,

    Thanks for writing back.

    You can also try calling BeginUpdate and EndUpdate before and after the update occurs. Anyway, I am afraid that considering the fact that the ObservableCollection does not expose a RemoveRange method and we therefore could not track which items have been removed, you will have to use some kind of trickery to achieve better performance.

    Regards,
    Deyan
    Telerik

    DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

Back to Top