I am working in WinForms with a ListBox. I have questions about the DisplayMember property.
If I understand correctly - if I do not provide a DisplayMember property then the ListBox uses the objects ToString() method. However if I specify DisplayMember then the ListBox will show the specified property of the objects in the collection.
However, is it possible to add some static text?
What I mean is, if my Property returns a first name such as "Dave", is there a way for me to prefix this with "First Name: Dave" through using the DisplayMember property or some other means?
I ask because the collection of objects I am working with is from a class I don't have control over - therefore I can't easily create a new property or override the ToString() method.
I thought briefly about extending the class and providing my own overridden ToString() method. However in my case I would need the ability to cast the parent class to the child class (which is not possible). I thought shortly about writing a static method that would accept the parent class as a parameter, copy its contents and return the child class but I just don't know if that is considered clean and best practice.
Any thoughts?
Aucun commentaire:
Enregistrer un commentaire