Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a ListBox which is made up of Grid Items in Multiple SelectionMode in Silverlight 3.0.

When I use ListBox.SelectedIndex it only returns the first item which is selected.

I would like to be able see all of the selected items such that it would return all of the selected item indexes' such as; 2, 5, and 7, etc.

Any help?

Cheers,

Turtlepower.

like image 451
turtlepower Avatar asked Oct 01 '10 03:10

turtlepower


People also ask

How do I get ListBox index?

To retrieve a collection containing the indexes of all selected items in a multiple-selection ListBox, use the SelectedIndices property. If you want to obtain the item that is currently selected in the ListBox, use the SelectedItem property.

How do I list multiple values in ListBox?

First, you need to set the SelectionMode property on your ListBox to either SelectionMode. MultiSimple or SelectionMode. MultiExtended (so that you can select multiple items). Next, you need to add an event handler for the SelectedIndexChanged event on your ListBox .

How get multiple values from ListBox in asp net?

You can use the ListBox. GetSelectedIndices method and loop over the results, then access each one via the items collection. Alternately, you can loop through all the items and check their Selected property.

How do you get the selected items and search the ListBox items?

To retrieve a collection containing all selected items in a multiple-selection ListBox, use the SelectedItems property. If you want to obtain the index position of the currently selected item in the ListBox, use the SelectedIndex property.


1 Answers

You can find the selected indexes by iterating through SelectedItems and finding the objects in the Items property, like this:

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();
like image 196
Yogesh Avatar answered Oct 18 '22 06:10

Yogesh