问题
In a GTK/GTK# TreeView
, how do I get the item/node which the mouse pointer is currently hovering over?
回答1:
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);
来源:https://stackoverflow.com/questions/9556389/how-do-i-get-the-item-tree-node-under-the-mouse-pointer-in-a-treeview