代码之家  ›  专栏  ›  技术社区  ›  Malfist

组合框的组合显示成员

  •  1
  • Malfist  · 技术社区  · 14 年前

    我需要在一个组合框中显示多个数据块,但我不知道该怎么做。

    下面是我要做的代码:

            innerBox.DisplayMember = @"t => t.TenantName + ""\t"" + t.Property.PropertyName + ""\t"" + t.RentalUnit.UnitNumber ";
    

    但它不起作用,尽管如此:

            innerBox.DisplayMember = @"t => t.TenantName";
    

    我怎样才能让复合材料工作?

    3 回复  |  直到 14 年前
        1
  •  2
  •   SLaks    14 年前

    这是不可能的。

    相反,您应该向基础对象添加一个属性。

        2
  •  2
  •   arbiter    14 年前

    DisplayMember只能包含单个属性名!如果需要复合输出,则应订阅 Format 事件并用代码编写输出字符串。

        3
  •  0
  •   JonBaron    14 年前

    我想在DisplayMember中使用“[代码][文本]”,并通过使用Linq添加属性来解决它:

    var actionCodes = pps.GetAllActionCodes();
    if (actionCodes != null)
    {
        var actionCodesNew = (from c in actionCodes
                                    select new
                                    {
                                        c.Code,
                                        c.Text,
                                        CodeAndDesc = string.Format("{0} {1}", c.Code, c.Text).Trim()
                                    }).ToArray();
                comboBox.Items.AddRange(actionCodesNew);
                comboBox.DisplayMember = "CodeAndDesc";
            }
    }
    

    当性能不是问题时工作正常。:)