WPF在鼠标下获取元素
时间:2020-03-05 18:48:13 来源:igfitidea点击:
WPF是否可以通过MouseMove事件在鼠标下获取元素数组?
解决方案
回答
可以使用VisualTreeHelper.HitTest吗?
http://lukieb.blogspot.com/2008/07/visualtreehelperhittest.html
回答
我们也可以尝试使用Mouse.DirectlyOver属性获取鼠标下方的最顶部元素。
回答
从" WPF释放",页383:
Visual hit testing can inform you about all Visuals that intersect a location, [...] you must use [...] the [VisualTreeHelper.]HitTest method that accepts a HitTestResultCallback delegate. Before this version of HitTest returns, the delegate is invoked once for each relevant Visual, starting from the topmost and ending at the bottommost.
这样的回调的签名是
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文档中的" VisualTreeHelper.HitTest"。