如何在TreeView中的鼠标指针下获取项目(树节点)? [英] How do I get the item (tree node) under the mouse pointer in a TreeView?

查看:187
本文介绍了如何在TreeView中的鼠标指针下获取项目(树节点)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GTK / GTK# TreeView 中,如何获得鼠标指针当前悬停的项目/节点?

解决方案

假设我们要使用鼠标右键选择项目而不使用复选框。下面的 ButtonPress 事件处理程序就是这样做的 - 它切换我们用人民币点击的项目的选定属性。然后,我们使用 CellDataFunc 以突出显示所选项目。 tv 是TreeView, store 是潜在的ListStore。

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

TreePath路径;
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();





$ b

如果我们使用排序的TreeView,我们必须使用TreeModelSort对象的ListStore对象来获取正确的项目:

pre $ $ $ $ c $ if(!sorted.GetIter(out iter,path))
返回;
Item item =(Item)sorted.GetValue(iter,0);


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

解决方案

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);

这篇关于如何在TreeView中的鼠标指针下获取项目(树节点)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆