C#: Binding hashtable to combo box question
public class FontType
{
...
public String Name { get { return _name; } }
public String DisplayName { get { return _displayName; } }
public Font UseFont { get { retur开发者_如何学JAVAn _font; } }
}
bindFontTypes.DataSource = availableFonts;
comboFontType.DataSource = bindFontTypes;
comboFontType.ValueMember = "Key";
comboFontType.DisplayMember = ...???;
Here, bindFontTypes
is BindingSource. availableFonts
is a Hashtable where Keys are strings, and Values are objects of FontType. For comboFontType.DisplayMember
I want to use objects' .DisplayName property. How do I specify that? Is it possible?
It might work if you set
comboFontType.DisplayMember = "Value"; // FontType
and overload ToString()
for FontType
.
As an alternative for ToString() you can handle the Format event of the combobox.
But I'm not even sure if the databinding works this way.
By using DisplayMember = "Value.DisplayName"
I am geting the last one added to the Hashtable...I am working on getting them all....
This is what I did...but only get the last item in the Hashtable to bind....
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";
精彩评论