Getting the index of multiple selected items in a list using Silverlight

I have a ListBox that consists of Grid elements in Multiple SelectionMode in Silverlight 3.0.

When I use ListBox.SelectedIndex, it returns only the first item that was selected.

I would like to see all the selected elements in such a way that it returns all the indices of the selected elements, for example; 2, 5 and 7, etc.

Any help?

Greetings

Turtlepower.

+5
source share
1 answer

You can find selected indices, iterating through SelectedItemsand searching for objects in a property Items, for example:

List<int> selectedItemIndexes = new List<int>();
foreach (object o in listBox.SelectedItems)
    selectedItemIndexes.Add(listBox.Items.IndexOf(o));

Or if you prefer linq:

List<int> selectedItemIndexes = (from object o in listBox.SelectedItems select listBox.Items.IndexOf(o)).ToList();
+8
source

All Articles