0

I need to move items from one listbox to another listbox on button click event in silverlight Application.

I use the follow code,

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ListBox2.Items.Add(ListBox1.SelectedItem);

            if (ListBox2.SelectedIndex != -1)
            {
                ListBox1.Items.Add(ListBox2.SelectedValue);
                ListBox2.Items.Remove(ListBox2.SelectedValue);
            }

        }

But If I try to use that above code it give the following Error,

operation not supported on read-only collection

How can solve this problem ??

2
  • @Michay No .. I diddnt use
    – V.V
    Commented Jul 3, 2015 at 10:05
  • @Michay OP probably didn't do that, because there is no DataSource property in WPF or Silverlight. The question is not about WinForms.
    – Clemens
    Commented Jul 3, 2015 at 10:17

1 Answer 1

2

You should use data binding to bind ObservableCollections of items to the ListBox.ItemsSource properties of your two ListBoxes:

<ListBox ItemsSource="{Binding Your1stCollectionProperty}" ... />

<ListBox ItemsSource="{Binding Your2ndCollectionProperty}" ... />

Then to move items, you just adjust the actual collections rather than try to adjust the ListBoxItems:

var itemToMove = Your1stCollectionProperty.ElementAt(indexOfItemToRemove);
Your1stCollectionProperty.Remove(itemToMove);
Your2ndCollectionProperty.Add(itemToMove);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.