【发布时间】:2015-01-17 15:40:03
【问题描述】:
我有一个包含一些字段(例如 field1、field2 等)的数据存储类,我想在 ComboBoxEntry 中显示此类的列表。我为 IEnumerable 创建扩展方法:
public static ListStore ToListStore<T>(this IEnumerable<T> Parent)
{
ListStore returnedValue = new ListStore(typeof (object));
//ListStore returnedValue = new ListStore (typeof(string));
if (Parent != null)
{
foreach (T node in Parent)
{
returnedValue.AppendValues (node);
//next two lines is just for testing.
TreeIter hello;
bool res = returnedValue.GetIterFirst (out hello);
}
}
return returnedValue;
}
然后我用下一种方式填充 ComboBoxEntry:
public static void SetGenericModel<T>(this ComboBox Parent, IEnumerable<T> Model, CellLayoutDataFunc LayoutFuncForModel)
{
ListStore store = Model.ToListStore ();
/*ListStore store = new ListStore (typeof(string));
store.AppendValues("a");
store.AppendValues("b");*/
CellRendererText genericClassCell = new CellRendererText();
//Parent.Clear ();
Parent.PackStart(genericClassCell, true);
Parent.SetCellDataFunc(genericClassCell, LayoutFuncForModel);
TreeIter treeIter;
store.GetIterFirst (out treeIter);
Parent.Model = store;
/*int columnsCount = Parent.Model.NColumns;
Parent.Model.GetIterFirst (out treeIter);
object obj = Parent.Model.GetValue (treeIter, columnsCount - 1);
Parent.Clear ();*/
}
好的,输出工作正常,但是当我尝试选择 ComboBoxEntry 的某些项目时,我的应用程序崩溃了,没有一些例外,只有输出窗口包含一些信息:*"(MyAppName:14822): GLib-GObject-WARNING ** :无法从“GtkSharpValue”类型的值设置“gchararray”类型的属性“文本”*。但是如果我将 ListStore 创建为字符串(注释代码):
ListStore store = new ListStore (typeof(string));
store.AppendValues("a");
store.AppendValues("b");
然后总是工作得很好。 我的代码有什么问题,如何解决这个问题(我应该用我的对象填充 ComboBoxEntry,因为当用户选择某个项目时,我应该将其作为数据存储区的项目处理)?
【问题讨论】:
标签: c# combobox mono cross-platform gtk#