我能想到的只是与ComboBox.GetEnumerator或类似的东西有关.
我想做的事情如下:
System.Collections.IEnumerator e = this.task_difficulty_combobox.GetEnumerator();
while (e.MoveNext())
{
if (e.ToString() == this.task.Difficulty.ToString())
{
Gtk.TreeIter i = (Gtk.TreeIter)e.Current;
this.task_difficulty_combobox.SetActiveIter(i);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.
你的代码不起作用的原因是"组合框中的项目"实际上是用于显示数据列的单元格渲染器.要获取实际数据,您需要TreeModel对象.
如果你真的必须只根据组合中的内容进行选择,那么你可以这样做:
string[] values = new string[]{"one", "two", "three"};
var combo = new ComboBox(values);
Gtk.TreeIter iter;
combo.Model.GetIterFirst (out iter);
do {
GLib.Value thisRow = new GLib.Value ();
combo.Model.GetValue (iter, 0, ref thisRow);
if ((thisRow.Val as string).Equals("two")) {
combo.SetActiveIter (iter);
break;
}
} while (combo.Model.IterNext (ref iter));
Run Code Online (Sandbox Code Playgroud)
但是,通常将您的值保持索引更为简洁,如下所示:
List<string> values = new List<string>(){"one", "two", "three"};
var combo = new ComboBox(values.ToArray());
// Select "two"
int row = values.IndexOf("two");
Gtk.TreeIter iter;
combo.Model.IterNthChild (out iter, row);
combo.SetActiveIter (iter);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8133 次 |
| 最近记录: |