c#winforms组合框显示成员

本文关键字:成员 显示 组合 c#winforms | 更新日期: 2023-09-27 17:59:41

我需要显示内部对象的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。

c#winforms组合框显示成员

我不确定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";
    }
}