开发者

Databinding a List of custom objects to ComboBox collection

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 wit开发者_如何学运维h 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. :)


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜