2012-02-21 58 views
1

我正在使用GTK尖銳的應用程序。我有這個代碼,但combobox1不顯示任何項目。爲什麼不?爲什麼我的組合框不顯示文本?

ListStore store = new ListStore(typeof(myclass)); 

store.AppendValue(new myclass("hola",7)); 
store.AppendValue(new myclass("hola2",8)); 
store.AppendValue(new myclass("hola3",2)); 

combobox1.Model = store; 

myclass覆蓋ToString()

+0

爲什麼你沒有爲combobox'combobox1.DisplayMember =「description」設置下列屬性; combobox1.ValueMember =「id」;'? – 2012-02-21 05:05:38

回答

0

我真的不知道,但要確保列表框鍵和值映射到類的字段。我認爲它需要具體。設置完值後,確保執行最後一個數據綁定,如:control.DataBind();一般情況下,C#綁定如下所示:1)自動列生成/手動將所有字段映射到鍵和值2)。設置字段3並調用bind()函數。

2

什麼你正在尋找定製Gtk.CellRenderer是:

private void MyClassRenderer(CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) 
{  
    MyClass myclass = model.GetValue(iter, 0) as MyClass; 
    (cell as CellRendererText).Text = myclass.ToString(); 
} 

隨着設置方法類似這樣的一些額外的代碼:

CellRendererText myClassCell = new CellRendererText(); 
combobox1.PackStart(myClassCell, true); 
combobox1.SetCellDataFunc(myClassCell, MyClassRenderer); 

ListStore store = new ListStore(typeof(MyClass)); 

store.AppendValues(new MyClass("hola",7)); 
store.AppendValues(new MyClass("hola2",8)); 
store.AppendValues(new MyClass("hola3",2)); 

combobox1.Model = store; 

確保SetCellDataFunc方法後PackStart方法調用。

工作完成! :)

+0

這是一個很好的答案。 – Luka 2013-08-28 10:20:26

相關問題