c#winforms组合框显示成员

时间:2014-04-23 12:49:53

标签: c# winforms datasource bind

我需要从内部对象

显示Name属性
public class CustomerLanguage : IDbIdentity 
{
    public Guid Id { get; set; }
    public virtual DocumentLanguage DocumentLanguage { get; set; }
}

 public class DocumentLanguage : IDbIdentity 
 {
    public string Name { get; set; }
 }

    public List<CustomerLanguage> CurrentCustomerLanguageList
    {
        get { return _currentCustomerLanguageList; }
        set
        {
            _currentCustomerLanguageList = value;
            bsCustomerLanguages.DataSource = value;
            cbLanguage.DataSource = bsCustomerLanguages.DataSource;
            cbLanguage.DisplayMember = "DocumentLanguage.Name";
            cbLanguage.ValueMember = "Id";
        }
    }

因此ValueMember必须是CustomerLanguage.Id,而DisplayMember必须是DocumentLanguage.Name。 如何设置DisplayMember? 当前代码显示guid。

1 个答案:

答案 0 :(得分:1)

我不确定winforms是否支持嵌套属性绑定,我猜它不支持。但你总是可以像这样添加代理属性。

public class CustomerLanguage : IDbIdentity 
{
    public Guid Id { get; set; }
    public virtual DocumentLanguage DocumentLanguage { get; set; }
    public string Name { get { return DocumentLanguage.Name; } }
}

public List<CustomerLanguage> CurrentCustomerLanguageList
{
    get { return _currentCustomerLanguageList; }
    set
    {
        _currentCustomerLanguageList = value;
        bsCustomerLanguages.DataSource = value;
        cbLanguage.DataSource = bsCustomerLanguages.DataSource;
        cbLanguage.DisplayMember = "Name";//Just name
        cbLanguage.ValueMember = "Id";
    }
}