C#:ハッシュテーブルをコンボボックスの質問にバインドする
-
05-07-2019 - |
質問
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
は、キーが文字列、値がFontTypeのオブジェクトであるハッシュテーブルです。 comboFontType.DisplayMember
の場合、オブジェクトの.DisplayNameプロパティを使用します。どのように指定しますか?可能ですか?
解決
設定すると機能する可能性があります
comboFontType.DisplayMember = "Value"; // FontType
および ToString()
を FontType
にオーバーロードします。
ToString()の代替として、コンボボックスのFormatイベントを処理できます。
しかし、データバインディングがこのように機能するかどうかはわかりません。
他のヒント
DisplayMember =" Value.DisplayName"
を使用して、ハッシュテーブルに最後に追加されたものを取得しています...それらすべてを取得する作業をしています...
これは私がやったことです...しかし、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";
所属していません StackOverflow