【问题标题】:ListBox (multiselect) selected indexesListBox(多选)选定的索引
【发布时间】:2015-08-24 20:06:58
【问题描述】:

我有一个支持Multi-SelectListBox,我需要一种将所有选定索引作为List<int> 检索的方法。

例如:

Listbox
{
    item 1 - not selected
    item 2 - selected
    item 3 - selected
    item 4 - not selected
}

所以选定的索引List<int> 将如下所示:

List<int>() { 1, 2 };

【问题讨论】:

  • 类似“字符串文本 = listBox1.GetItemText(listBox1.SelectedItem);”也许?

标签: c# wpf collections listbox


【解决方案1】:

试试这个:

List<int> list = new List<int>();

foreach (var item in listBox1.SelectedItems)
{
   list.Add(listBox1.Items.IndexOf(item));// Add selected indexes to the List<int>
}

或者使用 linq:

List<int> list = (from object obj in listBox1.SelectedItems 
                  select listBox1.Items.IndexOf(obj)).ToList();

【讨论】:

  • 如果两个选中的项目相等怎么办? IndexOf() 不会返回第一个索引两次吗?
  • 如果选择中包含重复的元素,您需要执行类似this 的操作。
猜你喜欢
  • 1970-01-01
  • 2012-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多