WPF Listbox.selecteditems按照选择的顺序返回项目

时间:2021-12-11 16:04:07

I was debugging a co-workers program and ran across this issue in WPF.

我正在调试一个同事程序,并在WPF中遇到了这个问题。

It looks like the listBoxName.SelectedItems returns a list of selected items, in the order a user selects the item from the interface. This is a problem because I need to preserve the actual order the items.

看起来listBoxName.SelectedItems按用户从界面中选择项目的顺序返回所选项目的列表。这是一个问题,因为我需要保留项目的实际顺序。

Example:

the listbox is in Extended selectmode and my listbox contains something: runfirst, runsecond, runthird

列表框是扩展选择模式,我的列表框包含一些内容:runfirst,runsecond,runthird

the user is given an option to select what they want to run based from the listbox. They select runthird then runfirst. This causes runthird to appear at the top of the list and then runfirst. I guess i could sort the list before running a foreach but i was wondering if there is an easier way.

用户可以选择从列表框中选择要运行的内容。他们选择了runthird然后runfirst。这会导致runthird出现在列表的顶部,然后runfirst。我想我可以在运行foreach之前对列表进行排序,但我想知道是否有更简单的方法。

Thanks

3 个解决方案

#1


9  

What I did was use LINQ to return the selected items in index order. It is in VB.Net syntax, but it should be easy to fix up for C#.

我所做的是使用LINQ以索引顺序返回所选项目。它采用VB.Net语法,但它很容易修复C#。

Dim selecteditems = From selecteditem As ListBoxItem In ListBox1.SelectedItems _
                    Select selecteditem _
                    Order By ListBox1.Items.IndexOf(selecteditem)

#2


3  

Yeah I ended up iterating over all the items in the in the listbox and then checked if it was in selecteditems using contains as below

是的,我最终迭代列表框中的所有项目,然后检查它是否在选择项目中使用包含如下

            foreach (<yourobject> item in listForSelection.Items)
            {
                if (listForSelection.SelectedItems.Contains(item))
                {
                     \\code here
                }
            }

Thanks for the help guys

谢谢你的帮助

#3


1  

I think you gave the answer in your question.

我想你在问题中给出了答案。

Most of the time the order of selected items won't matter. Since it does for you, sorting the selected items before processing them seems to be the simplest solution. I can't really think of a simpler one, especially when the sorting should only add 1 line.

大多数情况下,所选项目的顺序无关紧要。既然它适合您,在处理它们之前对所选项目进行排序似乎是最简单的解决方案。我真的不能想到一个更简单的,特别是当排序应该只添加1行。

You can use the same Comparison<T> or IComparer<T> that was used to sort the original list. If you're binding to SelectedItems, you can use an IValueConverter to do the sorting.

您可以使用与原始列表排序相同的Comparison 或IComparer 。如果您绑定到SelectedItems,则可以使用IValueConverter进行排序。

#1


9  

What I did was use LINQ to return the selected items in index order. It is in VB.Net syntax, but it should be easy to fix up for C#.

我所做的是使用LINQ以索引顺序返回所选项目。它采用VB.Net语法,但它很容易修复C#。

Dim selecteditems = From selecteditem As ListBoxItem In ListBox1.SelectedItems _
                    Select selecteditem _
                    Order By ListBox1.Items.IndexOf(selecteditem)

#2


3  

Yeah I ended up iterating over all the items in the in the listbox and then checked if it was in selecteditems using contains as below

是的,我最终迭代列表框中的所有项目,然后检查它是否在选择项目中使用包含如下

            foreach (<yourobject> item in listForSelection.Items)
            {
                if (listForSelection.SelectedItems.Contains(item))
                {
                     \\code here
                }
            }

Thanks for the help guys

谢谢你的帮助

#3


1  

I think you gave the answer in your question.

我想你在问题中给出了答案。

Most of the time the order of selected items won't matter. Since it does for you, sorting the selected items before processing them seems to be the simplest solution. I can't really think of a simpler one, especially when the sorting should only add 1 line.

大多数情况下,所选项目的顺序无关紧要。既然它适合您,在处理它们之前对所选项目进行排序似乎是最简单的解决方案。我真的不能想到一个更简单的,特别是当排序应该只添加1行。

You can use the same Comparison<T> or IComparer<T> that was used to sort the original list. If you're binding to SelectedItems, you can use an IValueConverter to do the sorting.

您可以使用与原始列表排序相同的Comparison 或IComparer 。如果您绑定到SelectedItems,则可以使用IValueConverter进行排序。