Windows窗体数据绑定的DisplayMember自定义类的子属性窗体、自定义、绑定、属性

2023-09-03 15:18:45 作者:装纯不是单纯~

我想设置我的列表框的DisplayMember属性的Windows窗体项目,以嵌套类内部的泛型列表我绑定到属性。

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?

编辑:

我想还不如被通过WSDL文档生成时,修改这些类。

I am trying not to modify these classes as they are being generated via a WSDL Document.

推荐答案

没有,大部分的WinForms绑定不支持这样的子属性。

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.

检查生成的code;它应该(与任何最新版本的工具)是一个部分类;这意味着你可以在第二类文件中添加额外的成员,这样就不会破坏WSDL生成code - 即

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
        }
    }
}

您现在应该能够绑定到InfoDescription相当容易。

You should now be able to bind to "InfoDescription" fairly easily.