c# - 组合框中基类的 DisplayMember 属性

标签 c# .net combobox

我使用 Windows 窗体。我想要来自基类的 DisplayMember 相等属性? 我有课

public class MyViewModel
{
    public int Id { get; set; }
    public Type Type { get; set; }
}

我希望我的组合框显示 Type.Name。

List<MyViewModel> list = new List<MyViewModel>();
list.Add(new MyViewModel(){ Id = 1, Type.GetType(int)});
list.Add(new MyViewModel(){ Id = 2, Type.GetType(string)});
//how i must to config displayMember???
myComboBox.DisplayMember = "Type.Name";
myComboBox.ValueMember = "Id";
myComboBox.DataSoutce = list;

但我无法获取 Type.Name 以在组合框中显示。你能帮帮我吗?

最佳答案

ComboBox 有一个用于此目的的事件,称为 Format:

myComboBox.DisplayMember = "Type";//Notice this
myComboBox.Format += (s,e) => {
    e.Value = ((Type)e.Value).Name;
};

关于c# - 组合框中基类的 DisplayMember 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18512565/

相关文章:

.net - 我可以在 Visual Studio 2015 中打开旧的 Visual Studio 解决方案(2005、2008、2010、2013)而不升级项目吗?

c# - 防止调整大小选择聚焦 ComboBox 中的所有文本

c# - 如何在 Visual C# 中使用来自 mysql 的数据填充组合框

c# - 使用 css 在同一行上设置 2 组 Label 和 editorFor 框

c# - 为单元测试设置类的只读属性

c# - SelectPdf 仅在自定义页面上显示页脚

java - 如何修复 javafx 中的渲染错误(ComboBox、ListView)

c# - ASP.NET - 从数据库中检索图像列表并将它们显示在 View 中?

c# - 使用 Roslyn 引用 PCL 库会导致 .NET 版本问题

.net - 如何从数据集中删除数据表(有一堆关系)