2009-10-21 49 views
0
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屬性。我如何指定?可能嗎?C#:綁定散列表組合框問題

回答

1

如果設置

comboFontType.DisplayMember = "Value"; // FontType 

和過載ToString()FontType它可能會奏效。

作爲ToString()的替代方法,您可以處理ComboBox的Format事件。

但我甚至不確定數據綁定是否以這種方式工作。

+0

它的工作原理,謝謝! – flamey 2009-10-21 15:40:42

1

通過使用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"; 
+0

這給了我「某個地方沒有設置對象實例的對象引用」。這不是獨立的程序,調試很痛苦。另一個解決方案工作。但謝謝你的回覆。 – flamey 2009-10-21 15:35:21