在我向GTK树视图添加一些项目之后,我需要从代码中选择列表中的第一项来激活treeviewcursorchanged事件,即:当点击树视图项时。我需要选择第一个树视图项。但是没有选择事件。 我用字符串填充树视图
liststore = new ListStore (typeof(string));
treeview1.Model = liststore;
答案 0 :(得分:2)
您必须使用与Gtk.TreeSelection
相关联的Gtk.TreeView
。要选择第一行,请按以下步骤操作:
Gtk.TreeIter iter;
if (liststore.GetIterFirst(out iter))
treeview1.Selection.SelectIter(iter);
以下是创建列表视图/模型的完整示例,向选择更改事件添加信号处理程序,然后将第一行设置为选中:
var view = new Gtk.TreeView();
var cell = new Gtk.CellRendererText();
var col1 = new Gtk.TreeViewColumn();
col1.Title = "Column 1"
col1.PackStart(cell, true);
col1.AddAttribute(cell, "text", 0);
view.AppendColumn(col1);
var model = new Gtk.ListStore(typeof(string));
model.AppendValues("AAA");
model.AppendValues("BBB");
model.AppendValues("CCC");
view.Model = model;
// This is the selection changed handler: I use a lambda but
// it is as easy to use a delegate `Changed` is a standard C#
// event.
view.Selection.Changed += (sender, e) => {
Console.WriteLine("SELECTION WAS CHANGED");
Gtk.TreeIter selected;
if (view.Selection.GetSelected(out selected)) {
Console.WriteLine("SELECTED ITEM: {0}", model.GetValue(selected, 0)));
}
};
Gtk.TreeIter iter;
if (model.GetIterFirst(out iter))
view.Selection.SelectIter(iter);
答案 1 :(得分:0)
GtkTreeView
中的选择逻辑被分成了自己的类GtkTreeSelection
。每个GtkTreeView
都有自己的GtkTreeSelection
,可通过gtk_tree_view_get_selection()
获得。
获得GtkTreeSelection
后,您需要做的只是获取要选择的行的GtkTreeIter
并致电gtk_tree_selection_select_iter()
。您还可以构建GtkTreePath
并选择gtk_tree_selection_select_path()
。
您如何获得GtkTreeIter
或GtkTreePath
取决于您。 These are the methods that apply to GtkTreePath
s and GtkTreeModel
s. GtkListStore
也是GtkTreeModel
和has its own methods。