C#ListBox ObservableCollection< T>
我正在尝试使用ListBox.DataSource = ObservableCollection,但是我不知道如何在OC更新时如何自动更新列表框.我可以在OC上挂接CollectionChanged事件,但是我需要对列表框执行什么操作才能使其更新?
I'm trying to use a ListBox.DataSource = ObservableCollection, however I can't figure out how to have the listbox automatically update when my OC updates. I can hook the CollectionChanged event on the OC, however what do I need to do to the listbox to make it update?
根据您的问题,听起来您正在尝试在WinForms应用程序中使用ObservableCollection<T>
.
Based on your question, it sounds like you're trying to use ObservableCollection<T>
in a WinForms application.
ObservableCollection<T>
主要用于WPF开发.在WinForms中,控件会在集合更改时自动更新,您的集合需要实现IBindingList
.
ObservableCollection<T>
is primarily used in WPF development. In WinForms, for the control be automatically updates as the collection changes your collection needs to implement IBindingList
.
最简单的解决方案是使用BindingList<T>
而不是ObservableCollection<T>
.之后,您的控件应随集合的更改而更新.
The easiest solution is to use BindingList<T>
instead of ObservableCollection<T>
. After that, your controls should update as the collection changes.