I'm trying to set the DisplayMember Property of my ListBox in a windows forms project to a property of a nested class inside a Generic List I am binding to.
Here's a simple example:
public class Security
{
public int SecurityId { get; set;}
public SecurityInfo Info { get; set;}
}
public class SecurityInfo
{
public string Description { get; set;}
}
//........//
public void DoIt()
{
List<Security> securities = new List<Security>();
//add securities to list
lstSecurities.DataSource = securities;
lstSecurities.DisplayMember = "Info.Description";
}
Is this possible with a simple ListBox or will I have to create a subclassed ListBox to handle this?
edit:
I am trying not to modify these classes as they are being generated via a WSDL Document.
No, most winforms bindings do not support child properties like this.
You can do it directly with custom type-descriptors, but that is a lot of work and not worth it.
Check the generated code; it should (with any recent version of the tools) be a partial class
; that means you can add extra members in a second class file, so you don't break the wsdl-generated code - i.e.
namespace MyWsdlNamespace {
partial class MyClass {
public string InfoDescription {
get { return Info.Description; }
// and a set if you want
}
}
}
You should now be able to bind to "InfoDescription" fairly easily.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With