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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:56:41+00:00 2026-06-16T18:56:41+00:00

I have two listviews. One of left handside and another on right hand side.

  • 0

I have two listviews. One of left handside and another on right hand side. I have two buttons to add and remove items from the two listviews.

LHSListview is bound to List and RHSListview is bound to List. Column class has two variables ‘order’ and ‘Id’.

when I click on the add button all the selected items from LHSListview must move to RHSListview. And vice versa when clicked on remove button.

This is what I am trying to do on the click of add button

var list1 = new ArrayList(lstAllFields.SelectedItems);
            foreach (var item in list1)
            {
                lstAllFields.Items.Remove(item);
                SelectedFields.Items.Add(item);
            }

But this throws an error on lstAllFields.Items.Remove(item); this line saying “Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.”

  • 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-16T18:56:42+00:00Added an answer on June 16, 2026 at 6:56 pm

    You mentioned you’re using MVVM, so you probably know that you shouldn’t be changing the items from the ListViews inside the view. What you should do is modify the collection you are bound to in the ViewModel.

    Problem is it’s kind of tricky to get the multiple selections in MVVM, because the SelectedItems property isn’t a Dependency Property.

    There are 2 ways to achieve what you’re after, both support MVVM:
    The shorter and easier way is to listen to the Button_Click in the View’s CodeBehind, create a new list of the selected items and pass it to the VM to do the logic of adding and removing items.

    So a short version would look like this:

    Code Behind:

    private void MyButton_OnClick(object sender, RoutedEventArgs e)
    {
       List<MyObject> mySelectedItems = new List<MyObject>();
    
       foreach (MyObject item in listview1.SelectedItems)
       {
           mySelectedItems.Add(item);
       }
    
       (this.DataContext as MainVM).MoveMethod(mySelectedItems);
     }
    

    View Model (in my class I called it MainVM)

    public void MoveMethod(List<MyObject> selected)
    {
       foreach (var item in selected)
       {
          List1.Remove(item);
          List2.Add(item);
        }
    }
    

    That’s it. Just remember, the List1 and List2 (which are the ItemSource‘s that ListView1 and ListView2 bind to, must be ObservableCollection to see the update in the UI.

    I promised a longer option too, for that see the great 3-part blog post on the subject:

    • MVVM and Multiple Selection – Part I
    • MVVM and Multiple Selection – Part II
    • MVVM and Multiple Selection – Part III
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two List<ColumnClass> . one for left side listview and another for right
I have two listviews and two buttons(add and remove). I can select an item
I have two listviews, one where the data is displayed and another where I
I have a tab contol with two tabs. Each one containing listviews. When the
I have two activities one displaying map view and another listview, the main objective
I have to add two columns in a ListView . One column shows different
I have two listViews on the page. I select Item in the first one
I have a linear layout with two listviews and two dead buttons. How to
I have a Listview that originally listed items from a menu item with one
I have two custom ListViews . One of them consists of an ImageView and

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.