public class FontType
    {
        ...
        public String Name { get { return _name; } }
        public String DisplayName { get { return _displayName; } }
        public Font UseFont { get { return _font; } }
    }


bindFontTypes.DataSource = availableFonts;
comboFontType.DataSource = bindFontTypes;
comboFontType.ValueMember = "Key";
comboFontType.DisplayMember = ...???;

这里, bindFontTypes 是BindingSource。 availableFonts 是一个Hashtable,其中Keys是字符串,Values是FontType的对象。对于 comboFontType.DisplayMember ,我想使用对象的.DisplayName属性。我该如何指定?有可能吗?

有帮助吗?

解决方案

如果设置

,它可能会有效
comboFontType.DisplayMember = "Value";  // FontType

并重载 ToString()以获取 FontType

作为ToString()的替代方法,您可以处理组合框的Format事件。

但我甚至不确定数据绑定是否以这种方式工作。

其他提示

使用 DisplayMember =" Value.DisplayName" 我正在考虑添加到Hashtable的最后一个...我正在努力让它们全部......

这就是我所做的......但只能让Hashtable中的最后一项绑定....

BindingSource src = new BindingSource();
            src.DataSource = new Hashtable 
            { 
            {
                "blah", 
                new FontType 
                { 
                    Name = "newFont", 
                    DisplayName = "new Font" 
                } 
                },
                { 
                    "another", 
                    new FontType 
                    {
                        Name = "anotherFont",
                        DisplayName = "another Font" 
                    } 
                    } 
            };
            comboBox1.DataSource = src;
            comboBox1.ValueMember = "Key";
            comboBox1.DisplayMember = "Value.DisplayName";
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top