tags:

views:

44

answers:

3

I'm working on a project that controls multiple devices over USB and intercepts the WM_DEVICECHANGE events from Windows to manage currently connected devices and plugin modules using those devices. There exists a combobox on the main form that (should) display all available devices (the custom object) and be dynamically updated in the event of either device arrival or removal.

Now, every tutorial that I have read including some from here have directed me to set the DataSource property of the combobox to the List of objects that I have, and leave the DisplayMember/ValueMember property blank to display ToString() and to return the object as the value.

In some instances I have tried (such as post instantiating the List and populating it with sample/real objects) the combobox populates, however upon removal or arrival, the collection in the combobox does not update even when reassigning the List to the combobox's DataSource property after every device arrival/removal method.

EDIT: Oh right... the question...

How do I databind the collection of (custom objects returned as the values) dynamically such that it can handle sudden changes (additions/removals) to the list's contents?

EDIT 2: Sorry I didn't make this more clear, wasn't aware of how pervasive WPF has gotten, but this is a WinForms project.

Thanks in advance for any help you can provide. :)

+1  A: 

even when reassigning the List to the combobox's DataSource property after every device arrival/removal method.

After reassigning, you need to call .DataBind() again.

If you're using .net 3.5 or above, you can use an ObservableCollection to have the list update automagically.

Winston Smith
A: 

I'm not sure if you're using WinForms or WPF. I'm assuming WPF as nobody should start out on WinForms nowadays...


You need to change your List to an ObservableCollection<T>.

The WPF binding model relies heavily on a magic subsystem of fairies and warlocks (not mocking you, I'm actually serious). They have their own language for informing each other that something has changed between the DataContext and UI. These are primarily the DependencyObject/Dependency properties, INotifyPropertyChanged and INotifyCollectionChanged, and the ObservableCollection<T>.

By proper use of these totems, changes in your ViewModels/Models (the stuff you stick in the DataContext and/or bind to the DataSource) will be reflected on the UI.

Will
Unfortunately I am using WinForms right now as I am on a short term contract (< 4 months) and the organization I am working for does not really require the features and forward compatibility that come with WPF. Although they expressed interest in being more forward compatible in the future, for the time being, the project will have to be completed using WinForms and then probably converted slowly in future revisions. Its a simple application, so I don't see why not.
James
A: 

I don't know about that ToString() bit. I always set the DisplayMember to the Property I want to display. I don't care about the ValueMember because the SelectedItem is my Object.

DataBind doesn't apply if this is a WinForms project, which it sounds like. (USB and stuff)

I don't do anything funky in my project when up update the data in a ComboBox, I just set the DataSource again.

EDIT: You could try using a BindingList rather than a List also. http://msdn.microsoft.com/en-us/library/ms132679.aspx

Jeff Sheldon