在 WPF 中,如何获取与鼠标光标下方的树视图项关联的数据对象? [英] In WPF, how do I get the Data Object associated to the Tree View Item underneath the mouse cursor?

查看:24
本文介绍了在 WPF 中,如何获取与鼠标光标下方的树视图项关联的数据对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 WPF 应用程序中,我有一个树视图.此树视图绑定到自定义(即不是 TreeviewItems).所以我使用了一个hierarchyDataTemplate来控制树的渲染方式.

In my WPF application, I have a treeview. This treeview is bound to a custom class (i.e. not TreeviewItems). So I use a hierarchicalDataTemplate to control how the tree renders.

当我的鼠标悬停在树视图项上时,我想获取与树视图项关联的数据对象(即我的自定义类实例).我该怎么做?

When my mouse is over a tree view item, I would like to get the Data Object (i.e. my custom class instance) associated with the tree view item. How do I do this?

澄清 - 我需要鼠标光标下的数据对象(而不是 UIElement).

To clarify - I need the data object (not the UIElement) under the mouse cursor.

假设我检索数据对象的方法具有以下签名:

Assume my method to retrieve the data object has the following signature:

private object GetObjectDataFromPoint(ItemsControl source, Point point)
{
    ...
}

推荐答案

类似这样的事情(未经测试):

Something like this (untested):

private object GetObjectDataFromPoint(ItemsControl source, Point point)
{
    //translate screen point to be relative to ItemsControl
    point = _itemsControl.TranslatePoint(point);
    //find the item at that point
    var item = _itemsControl.InputHitTest(point) as FrameworkElement;

    return item.DataContext;
}

这篇关于在 WPF 中,如何获取与鼠标光标下方的树视图项关联的数据对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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