How do I get the item (tree node) under the mouse

2019-06-08 21:06发布

In a GTK/GTK# TreeView, how do I get the item/node which the mouse pointer is currently hovering over?

1条回答
小情绪 Triste *
2楼-- · 2019-06-08 21:58

Let's say we want to select items using the right mouse button without using checkboxes. The following ButtonPress event handler does just that - it toggles the selected property of the item we have clicked with the RMB. We then use CellDataFuncs to highlight the selected items. tv is the TreeView, store is the underlying ListStore.

[GLib.ConnectBefore]
void HandleTreeViewButtonPressEvent(object o, ButtonPressEventArgs args)
{
    if (args.Event.Button != 3)
        return;

    TreePath path;
    int x = Convert.ToInt32(args.Event.X);
    int y = Convert.ToInt32(args.Event.Y);
    if (!tv.GetPathAtPos (x, y, out path)) 
        return;

    TreeIter iter;      
    if (!store.GetIter(out iter, path)) 
        return;
    Item item = (Item) store.GetValue (iter, 0);

    item.Selected = !item.Selected;
    tv.QueueDraw();
}

If we are using a sorted TreeView, we have to use the TreeModelSort object instead of the ListStore object to get the correct item:

    if (!sorted.GetIter(out iter, path)) 
        return;
    Item item = (Item) sorted.GetValue (iter, 0);
查看更多
登录 后发表回答