How do I get the item (tree node) under the mouse pointer in a TreeView?

☆樱花仙子☆ 提交于 2019-12-10 12:13:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!