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 8055445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:23:13+00:00 2026-06-05T08:23:13+00:00

I have a question relating to modifying the individual items in an ObservableCollection that

  • 0

I have a question relating to modifying the individual items in an ObservableCollection that is bound to a ListBox in the UI.

The user in the UI can multiselect items and then drop them at a particular index to re-order them.

So, if I have items {0,1,2,3,4,5,6,7,8,9} the user can choose items 2, 5, 7 (in that order) and choose to drop them at index 3, so that the collection now becomes,

{0,1,3, 2, 5, 7, 4, 8,9}

The way I have it working now, is like this inside of ondrop() method on my control,
I do something like:

foreach (Item item in draggedItems)
{
   int oldIndex = collection.IndexOf(item.DataContext as MyItemType);
   int newIndex = toDropIndex;

  if (newIndex == collection.Count)
  {
         newIndex--;
  }

  if (oldIndex != newIndex)
  {
     collection.Move(oldIndex, newIndex);                                     
  }

}

But the problem is, if I drop the items before the index where I start dragging my first item, the order becomes reversed…so the collection becomes,

{0,1,3, 7, 5, 2, 4, 8,9}

It works fine if I drop after index 3, but if I drop it before 3 then the order becomes reversed.

Now, I can do a simple remove and then insert all items at the index I want to, but “move” for me has the advantage of keeping the selection in the ui (remove basically de-selects the items in the list..)….so I will need to make use of the move method,

what is wrong with my method above and how to fix it?
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. Editorial Team
    Editorial Team
    2026-06-05T08:23:15+00:00Added an answer on June 5, 2026 at 8:23 am
    foreach (Item item in draggedItems)
    {
        int oldIndex = collection.IndexOf(item.DataContext as MyItemType);
        int newIndex = Math.Min(toDropIndex, (collection.Count - 1));
    
        if (oldIndex == newIndex)
            continue;
    
        collection.Move(oldIndex, newIndex);
    
        if (oldIndex > newIndex)
            toDropIndex++;
    }
    

    This should help you out. You need to increase the toDropIndex so you’re moving each item in front of the last. Otherwise, you’re simply moving 2 into index 3, then 5 into index 3 (pushing 2 to index 4), then 7 into index 3 (pushing 2 to index 5 and 5 to index 4). This will, instead, place 2 at index 3, 5 at index 4, and 7 at index 5. The Math.Min() function will ensure that you’ll never be out of range.

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

Sidebar

Related Questions

I am hoping someone can help me with a question i have relating to
two relating question: I have a table that is in use in a production
I have this question relating to Lucene. I have a form and I get
I have a question relating to alignment in C/C++. In Determining the alignment of
I have a design/best practice question relating to MVC3. I have a front page
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have a question relating programming and english language both: Whether to use third
I have a question relating to the timer function. I have managed to find
I have a question relating to the usage of this. Suppose I have two
I have a question relating to properties for a specific instance of a CI

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.