首页 文章

WPF在鼠标下获取元素

提问于
浏览
32

WPF是否有办法在MouseMove事件上获取鼠标下的元素数组?

3 回答

  • 3

    从“WPF Unleashed”,第383页:

    视觉命中测试可以告知您所有与某个位置相交的视觉效果,您必须使用接受HitTestResultCallback委托的[VisualTreeHelper . ] HitTest方法 . 在此版本的HitTest返回之前,为每个相关的Visual调用一次委托,从最顶端开始到最底部结束 .

    这种回调的签名是

    HitTestResultBehavior Callback(HitTestResult result)
    

    并且它必须返回 HitTestResultBehaviour.Continue 以接收进一步的命中,如下所示(来自MSDN上的链接页面):

    // Return the result of the hit test to the callback.
    public HitTestResultBehavior MyHitTestResult(HitTestResult result)
    {
        // Add the hit test result to the list that will be processed after the enumeration.
        hitResultsList.Add(result.VisualHit);
    
        // Set the behavior to return visuals at all z-order levels.
        return HitTestResultBehavior.Continue;
    }
    

    有关详细信息,请参阅MSDN documentation for VisualTreeHelper.HitTest .

  • 36

    您还可以尝试使用Mouse.DirectlyOver属性来获取鼠标下最顶层的元素 .

相关问题