使用Silverlight获取列表框中多个选定项目的索引

问题描述:

我有一个ListBox,它由Silverlight 3.0中多个SelectionMode中的网格项组成。使用Silverlight获取列表框中多个选定项目的索引

当我使用ListBox.SelectedIndex时,它只返回所选的第一个项目。

我希望能够看到所有选定的项目,以便它将返回所有选定的项目索引,例如; 2,5和7等。

任何帮助吗?

干杯,

Turtlepower。

+0

你检查是否有是名为SelectedIndices/SelectedIndexList或类似的属性? – 2010-10-01 03:29:14

+0

在Silverlight中的ListBox没有SelectedIndices,但是非常感谢。 – turtlepower 2010-10-01 03:38:15

您可以通过SelectedItems迭代,并在Items属性找到对象,像这样的发现所选择的指标:

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

或者如果你喜欢LINQ:

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

谢谢Yogesh,它几乎工作。 奇怪的是,我只有5个项目在我的列表框中,当我将它们全部返回时,我得到7个项目变为“0,1,2,3,4,0,0,0”。为什么额外的三个0结束? – turtlepower 2010-10-01 03:38:45

+0

5个项目在选定的项目?你可以发布你正在使用的代码来“退还”吗? – Yogesh 2010-10-01 03:41:14

+0

List selectedItemIndexes = new List (); (myListBox.SelectItems中的对象o) { selectedItemIndexes.Add(myListBox.Items.IndexOf(o)); } 是的,5项,我也只选择了5项。奇。 – turtlepower 2010-10-01 03:42:15