【问题标题】:How to determine where the mouse is hovering relative to a parent element如何确定鼠标相对于父元素的悬停位置
【发布时间】:2012-04-19 22:13:52
【问题描述】:

我正在为支持拖放的树视图实现拖放。

树中有文件夹和文件。当我拖过文件夹底部的 25% 时,我希望它重新排序。当我拖动树视图项的顶部 75% 时,我希望它把内容放入文件夹中。

如何确定我在元素中的位置?这是我失败的尝试:

    private bool ShouldReorder(TreeViewItem targetItem)
    {
        var relativePosition = PointToScreen(Mouse.GetPosition(targetItem));
        return relativePosition.Y/targetItem.ActualHeight < 0.25;
    }

此代码的相对位置为负数。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    将其转换为屏幕坐标后,您可以将其转换回父控件的客户端坐标

    Parent.PointToClient(screenpt) ;
    

    【讨论】:

      【解决方案2】:

      我使用以下代码来消费全局鼠标事件(这段代码也可以获取全局x/y位置数据而不管窗口焦点):

      http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C

      我的程序在:

      http://www.codeproject.com/Articles/347795/Inactivity-Logoff

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-18
        • 1970-01-01
        • 2013-02-22
        • 2011-03-15
        • 1970-01-01
        相关资源
        最近更新 更多