Windows窗体数据绑定的DisplayMember自定义类的子属性 [英] Windows Forms Databinding DisplayMember a Custom Class's Child Property

查看:459
本文介绍了Windows窗体数据绑定的DisplayMember自定义类的子属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置我的列表框的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.

这篇关于Windows窗体数据绑定的DisplayMember自定义类的子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆