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"
أنا غيتينغ أضاف آخر واحد إلى جدول هاش ... أنا أعمل على الحصول على كل منهم ....
وهذا هو ما فعلته ... ولكن فقط الحصول على البند الأخير في جدول هاش لربط ....
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