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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:14:31+00:00 2026-06-11T18:14:31+00:00

I have a list box with a bunch of values in it. I also

  • 0

I have a list box with a bunch of values in it. I also have an UP button and a DOWN button.
With these buttons, I would like to move the selected item in the list box up/down. I am having trouble doing this.

Here is my code so far:

    private void btnDataUp_Click(object sender, RoutedEventArgs e)
    {

        int selectedIndex = listBoxDatasetValues.SelectedIndex; //get the selected item in the data list

        if (selectedIndex != -1 && selectedIndex != 0) //if the selected item is selected and not at the top of the list
        {
            //swap items here
            listBoxDatasetValues.SelectedIndex = selectedIndex - 1; //keep the item selected
        }


    }

I do not know how to swap the values! Any help would be GREATLY appreciated!

  • 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-11T18:14:32+00:00Added an answer on June 11, 2026 at 6:14 pm

    Since you have populated the listbox by binding to a ObservableCollection using ItemsSource, you cant modify the Items property of the listbox.

    ItemsSource can be set only when the Items collection is empty, and Items can be modified only when ItemsSource is null.

    Otherwise you will get the error “Operation is not valid while ItemsSource is in use…”

    What you have to do, is modify the underlying collection, and because it’s an ObservableCollection, the ListBox will reflect the changes.

    The following code shows how you can move an item up and down by swapping the item in the collection.

    The corresponding XAML just contains a listbox called lbItems and 2 buttons which hook up the eventhandlers.

    public partial class MainWindow : Window
    {
        private ObservableCollection<string> ListItems = new ObservableCollection<string>  
        { 
            "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"
        };
    
        public MainWindow()
        {
            InitializeComponent();
            lbItems.ItemsSource = this.ListItems;
        }
    
        private void up_click(object sender, RoutedEventArgs e)
        {
            var selectedIndex = this.lbItems.SelectedIndex;
    
            if (selectedIndex > 0)
            {
                var itemToMoveUp = this.ListItems[selectedIndex];
                this.ListItems.RemoveAt(selectedIndex);
                this.ListItems.Insert(selectedIndex - 1, itemToMoveUp);
                this.lbItems.SelectedIndex = selectedIndex - 1;
            }
        }
    
        private void down_click(object sender, RoutedEventArgs e)
        {
            var selectedIndex = this.lbItems.SelectedIndex;
    
            if (selectedIndex + 1 < this.ListItems.Count)
            {
                var itemToMoveDown = this.ListItems[selectedIndex];
                this.ListItems.RemoveAt(selectedIndex);
                this.ListItems.Insert(selectedIndex + 1, itemToMoveDown);
                this.lbItems.SelectedIndex = selectedIndex + 1;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list box that when an item is selected or deselected I
I have a list box like this, <asp:ListBox ID=ListBox1 runat=server Height=175px Width=213px> <asp:ListItem Value=all>All</asp:ListItem>
I have a list box and i am trying to get currently checked item
So I have a list box that displays averages in a table like format
Suppose I have a list box with different items, each with unique values, and
In my project, I have a list box. When I click an item on
I have list box control where I am binding the values to the listbox
I have List box in which I am displaying values at runtime. But i
I have a list box with items like A B C D E .
I have a class encapsulating a bunch of collections. I would like to bind

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.