简体   繁体   English

GTK#TreeView确定选择哪个项目

[英]Gtk# TreeView determine which item is selected

I am new to Gtk and I am using Gtk#. 我是Gtk的新手,正在使用Gtk#。 I am using a TreeView component and I wish to know when the user chooses a new entry. 我正在使用TreeView组件,我想知道用户何时选择新条目。

I have tried using the TreeView.Selection.SelectFunction. 我尝试使用TreeView.Selection.SelectFunction。 However, this gets called before the selection is made, which is not what I want. 但是,这是在进行选择之前调用的,这不是我想要的。 I want to know what the user has just selected, not what was selected before. 我想知道用户刚刚选择的内容,而不是之前选择的内容。 How do I go about accomplishing this? 我该如何完成这项工作?

Thanks 谢谢

This is what I currently have 这是我目前所拥有的

catalogTreeView.Selection.SelectFunction = CategorySelected;


bool CategorySelected(Gtk.TreeSelection selection, Gtk.TreeModel model, Gtk.TreePath path, bool abool) {
    TreeIter iter;


    if (selection.GetSelected (out model, out iter)) {
        Console.WriteLine("abool = " + abool);
        Console.WriteLine("Path of selected row = {0}", model.GetPath (iter));
        Console.WriteLine(model.GetValue (iter, 0));
    }

    return true;
}

You want a handler for the ButtonPressEvent in which you can use the treeview.Selection.GetSelected() operation to retrieve the selected model and iter as you are already. 您需要一个ButtonPressEvent的处理程序,可以在其中使用treeview.Selection.GetSelected()操作来检索所选模型并进行迭代。 NB. 注意 You need to add the GLib.ConnectBeforeAttribute attribute to this event handler and also add the event to the Events list for the treeview. 您需要将GLib.ConnectBeforeAttribute属性添加到此事件处理程序,也需要将该事件添加到树视图的“事件”列表中。 Eg 例如

treeview.Events |= EventMask.ButtonPressMask;
treeview.ButtonPressEvent += YourOnButtonPressEvent;

...

[GLib.ConnectBeforeAttribute]
protected void YourOnButtonPressEvent (object o, ButtonPressEventArgs args)
{
    // code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM