C# Gtk#:对象到ComboBoxEntry的列表存储
我有一个带有一些字段的数据存储类(例如field1、field2等),我想在ComboBoxEntry中显示这个类的列表。我为IEnumerable创建扩展方法:C# Gtk#:对象到ComboBoxEntry的列表存储,c#,combobox,mono,cross-platform,gtk#,C#,Combobox,Mono,Cross Platform,Gtk#,我有一个带有一些字段的数据存储类(例如field1、field2等),我想在ComboBoxEntry中显示这个类的列表。我为IEnumerable创建扩展方法: public static ListStore ToListStore<T>(this IEnumerable<T> Parent) { ListStore returnedValue = new ListStore(typeof (object));
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,因为当用户选择某个项目时,我应该将其作为数据存储的项目进行处理)?我想ComboxEntry默认情况下总是希望第一列是文本。也许这可以通过一些技巧来克服。但是一个文本条目期望值是文本,而不是图标或其他任何东西,这似乎是合乎逻辑的 现在,我知道Combobox(基类)默认情况下第一列是文本。您注释掉的这行代码:
Parent.Clear ();
可以清除任何默认设置并打包自己的渲染器
下一步是,当您想要读取组合框的内容时,必须使用树型选择器检索值,如下所示:
TreeIter iter;
if (Combo.GetActiveIter(out iter))
{
var value = (YourClass)this.Combo.Model.GetValue(iter, 0);
}
Parent.Clear ();
TreeIter iter;
if (Combo.GetActiveIter(out iter))
{
var value = (YourClass)this.Combo.Model.GetValue(iter, 0);
}