自排序列表框
完全被一些看似简单的事情难住了,而且已经被搞死了……但仍然难住了。
我想做的:我有一个 WinForms ListBox。它的项目填充了对象,并且设置了 DisplayMember。当应用程序运行时,列出的项目中的数据可能会发生变化,包括 DisplayMember 后面的字段。我希望列表框中显示的文本在发生这种情况时发生变化,并且我还希望列表框重新排序,以便项目保持按字母顺序排列。
BindingList 可以很好地在数据更改时更新显示的文本,但对于我来说,我无法对其进行排序。
我回顾了这一点:http://msdn.microsoft.com/en-us/library /ms993236.aspx
这里还有很多关于如何执行此操作的线程,但似乎都不适用于列表框。
在 ListBox 上设置 Sorted 属性同样没有帮助。
我需要做什么才能让 ListBox 自行排序?
Totally stumped by something that seems easy, and has been done to death... Yet still stumped.
What I want to do: I've got a WinForms ListBox. Its items are populated with objects, the DisplayMember is set. As the app runs, the data in the listed items might change, including the field behind the DisplayMember. I want the text displayed in the ListBox to change when this happens, and I also want the ListBox to re-sort itself so the items remain in alphabetical order.
A BindingList works fine to update the displayed text when the data changes, but for the life of me, I can't get it to sort.
I reviewed this: http://msdn.microsoft.com/en-us/library/ms993236.aspx
Plus numerous threads here about how to do this, but none of it seems to work for a ListBox.
Setting the Sorted property on the ListBox is similarly unhelpful.
What do I need to do to get a ListBox to sort itself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用 BindingSource 对象。
只需将其拖放到表单中并将 ListBox.DataSource 属性指向此 BindingSource 对象即可。
然后转到 BindingSource 的属性并根据需要定义排序。
然后,您可以在代码中设置 myBindingSource.DataSource = myCollection ,瞧,您的列表框已填充并排序。
简单的。
You can use a BindingSource object.
Just drag-n-drop it into your form and point your ListBox.DataSource property to this BindingSource object.
Then go to the BindingSource's properties and define Sort as you need.
Then in code you can set
myBindingSource.DataSource = myCollection
and voila, your listbox is populated and sorted.Easy.
与 Patrol02 的帖子一样,但是您可能需要尝试将 DataSource 设置为 null,然后根据列表大小更改触发的事件重新分配它。您可以在集合上使用观察者模式,重写 Add 和 Remove 方法来通知观察者重新绑定自身。
As with Patrol02's post, however you may want to try setting the DataSource to null and then reassigning it based on an event triggered by the list size changing. You could use the observer pattern on the collection, overriding the Add and Remove methods to notify watchers to rebind themselves.
重置数据源将有效地对列表框进行排序:
但这不是自动的。据我了解,只要通过事件或类似的事件更新 DisplayMember 后面的字段,就应该进行排序......
无论如何,请参阅我的完整测试:
Resetting the DataSource will effectively sort the ListBox:
But that's not automatic. As I understand, sorting should happen whenever the field behind the DisplayMember is updated, through an event or something like that...
See my complete test anyway:
列表控件上的 LVS_SORT 样式应该可以工作,但你说它不行。我会仔细检查它是否已应用。我从来没有遇到过自排序下拉列表控件的任何问题。请注意,我们所说的是列表控件,而不是列表视图控件。
The LVS_SORT style on the list control should work, but you say it doesn't. I would double check that it is applied. I've never had any trouble with a self-sorting drop-down list control. Note this is a list control we're speaking of, not a listview control.
我通过创建一个新类 BindingSortingList 来实现此目的,该类继承自 BindingList。在其中我重写了所有必要的方法,例如ApplySortCore()和RemoveSortCore()。当您应用排序时,您基本上必须在内部将其复制到具有排序功能的标准列表,对其进行排序,然后将其复制回“此”列表。这看起来很疯狂,但现在我有一个可重用的类用于此目的。
I did this by creating a new class, BindingSortingList, which inherited from BindingList. In it I overrode all the necessary methods, like ApplySortCore() and RemoveSortCore(). When you apply the sort, internally you basically have to copy it to a standard list, which has sorting ability, sort it, then copy it back into the "this" list. It seems crazy but now I have a reusable class for this purpose.
这个怎么办??
What about this??