tags:

views:

531

answers:

1

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?

+5  A: 

Based on your question, it sounds like you're trying to use ObservableCollection<T> in a WinForms application.

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.

The easiest solution is to use BindingList<T> instead of ObservableCollection<T>. After that, your controls should update as the collection changes.

MSDN: BindingList(T) Class

Justin Niessner
Perfect, that's what I was looking for!
WedTM
Or wait for .NET 4.0 to come out; `ObservableCollection<T>` will be moved to System.dll: http://blogs.msdn.com/bclteam/archive/2009/10/21/what-s-new-in-the-bcl-in-net-4-beta-2-justin-van-patten.aspx (see the last item)
Kyralessa