3

我使用 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。你能帮助我吗?

4

2 回答 2

7

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

myComboBox.DisplayMember = "Type";//Notice this
myComboBox.Format += (s,e) => {
    e.Value = ((Type)e.Value).Name;
};
于 2013-08-29T13:52:17.070 回答
1

您必须创建一个具有两个属性的新类:

class MyViewModel
{
    public string TypeName {get; set; }
    public string Id {get; set; }
}

// generate the datasource list.
List<MyViewModel> list = new List<MyViewModel>();
list.Add(new MyViewModel(){ Id = 1, TypeName = typeof(int).Name});
list.Add(new MyViewModel(){ Id = 2, TypeName = typeof(string).Name});

// how i must to config displayMember
myComboBox.DisplayMember = "TypeName";
myComboBox.ValueMember = "Id";
myComboBox.DataSoutce = list;
于 2013-08-29T13:57:38.033 回答