【问题标题】:In WPF, how do I get the Data Object associated to the Tree View Item underneath the mouse cursor?在 WPF 中,如何获取与鼠标光标下的树视图项关联的数据对象?
【发布时间】:2009-07-07 14:24:08
【问题描述】:

在我的 WPF 应用程序中,我有一个树视图。此树视图绑定到自定义 class(即 not TreeviewItems)。所以我使用了一个分层数据模板来控制树的呈现方式。

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

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

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

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

【问题讨论】:

    标签: wpf treeview


    【解决方案1】:

    类似这样的东西(未经测试):

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

    【讨论】:

    • 太棒了,这正是我想要的。谢谢肯特!
    【解决方案2】:
    private object GetObjectDataFromPoint(ItemsControl source, Point point) 
    {    
        //translate screen point to be relative to ItemsControl    
        point = source.TranslatePoint(point, source);    
    
        //find the item at that point    
        var item = source.InputHitTest(point) as FrameworkElement;   
    
        return item.DataContext;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      相关资源
      最近更新 更多