C#:将哈希表绑定到组合框问题
这里,C#:将哈希表绑定到组合框问题,c#,winforms,data-binding,combobox,hashtable,C#,Winforms,Data Binding,Combobox,Hashtable,这里,bindfontypes是BindingSourceavailableFonts是一个哈希表,其中键是字符串,值是FontType的对象。对于comboFontType.DisplayMember我想使用objects.DisplayName属性。我如何具体说明?可能吗?如果您设置 public class FontType { ... public String Name { get { return _name; } }
bindfontypes
是BindingSourceavailableFonts
是一个哈希表,其中键是字符串,值是FontType的对象。对于comboFontType.DisplayMember
我想使用objects.DisplayName属性。我如何具体说明?可能吗?如果您设置
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 = ...???;
和重载ToString()
用于FontType
作为ToString()的替代方法,您可以处理combobox的Format事件
但我甚至不确定数据绑定是否以这种方式工作。通过使用
displaymber=“Value.DisplayName”
我正在获取添加到哈希表中的最后一个数据绑定…我正在努力获取所有数据绑定
这就是我所做的…但只获取哈希表中要绑定的最后一项
comboFontType.DisplayMember = "Value"; // FontType
这给了我“对象引用未设置为对象实例”的信息。这不是一个独立的程序,调试起来很痛苦。另一个解决方案奏效了。但是谢谢你的回复。
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";