c# - 列表框(多选)选定索引

标签 c# wpf collections listbox

我有一个 ListBox支持Multi-Select我需要一种方法来检索所有选定的索引作为 List<int> .

例如:

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

所以选中的索引List<int>看起来像:
List<int>() { 1, 2 };

最佳答案

尝试这个:

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();

关于c# - 列表框(多选)选定索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32190848/

相关文章:

c# - 根据数组元素对数组的每 500 个元素进行分组

.net - WPF MVVM 使用命令与事件处理程序

wpf - 创建动画的非重复方式

java - foreach 语法中的三元运算符

loops - 集合中的元素在打印时被截断 - Smalltalk

c# - 将 List<int> 转换为 List<List<int>>

c# - 使用 PCL 和 Xamarin 对 Azure 移动服务执行单元测试时程序集版本不匹配

c# - 添加到 session ASP.NET C#

c# - 是否可以在 AfterMap 中调用 Mapper.Map?

c# - 如何为聚合属性实现 INotifyPropertyChanged