IListSource and ToString() with DataSource
-
05-07-2019 - |
Question
First I have a Listbox and set the DataSource to a MyObjectCollection
MyObjectCollection implements the Interface IListSource wich contains MyObject's
MyObject has the method
public override string ToString()
{
return "test";
}
The Listbox now displays "test" for each element in the MyObjectCollection.
But if I apply the IListSource interface to MyObject, too. The Listbox shows an empty string. How to fix that in the "MyObject" class. A workaround is to fix it in the Listbox Format event, but than every GUI element has to implement this workaround :/
Solution
Generally, things should either be lists, or should be an entity for binding. If it is a list (IList
or IListSource
) then much of the data-binding API will assume you actually want (for simple-binding scenarios, i.e. one row) the first item from the sublist.
I would simply change it so that MyObject
doesn't implement IListSource
, but encapsulates it, perhaps exposing it via a Items
property. You could also look at whether a custom TypeConverter
would work (I'll investigate...)