我可以在WPF画布中使用ZIndex较低的HitTest元素吗? [英] Can I HitTest element with lower ZIndex in WPF Canvas?
问题描述
当我在鼠标单击画布上使用 System.Windows.Media.VisualTreeHelper.HitTest
时,它总是使我返回顶部的元素。我的意思是,如果有两个元素相互重叠,我得到的就是画布中ZIndex较高的元素。
When I am using System.Windows.Media.VisualTreeHelper.HitTest
on mouse clicking canvas, it always returns me the element on top. I mean if there are two elements overlapping each other, the one I get is the one with higher ZIndex in canvas.
我也尝试做到了这种方式,但即使在我 IsHitTestVisible = False
它仍然向我返回了相同的那个。
I also tried to do it this way, but even after I made IsHitTestVisible = False
it still returned this same one to me.
是否有可能将元素置于下方 ?
Is there any possibility to get the element "underneath"?
推荐答案
请参见此处的命中测试和Z顺序部分:在可视层中进行命中测试,并在此处提供代码示例: VisualTreeHelper.HitTest方法。
See the section "Hit Testing and Z-Order" here: Hit Testing in the Visual Layer, and a code example here: VisualTreeHelper.HitTest Method.
简而言之,您需要使用 HitTest()
方法之一,并采用 HitTestResultCallback
,然后从您的回调中重新转动 HitTestResultBehavior。继续
直到找到所需的元素:
In short, you need to use one of the HitTest()
methods that take a HitTestResultCallback
, and then from your callback return HitTestResultBehavior.Continue
until you reach the element you're looking for:
Point pt = ...
VisualTreeHelper.HitTest(myCanvas, null,
new HitTestResultCallback(MyHitTestResult),
new PointHitTestParameters(pt));
...
private HitTestResultBehavior MyHitTestResult(HitTestResult result)
{
DoSomethingWith(result.VisualHit);
//Set the behavior to return visuals at all z-order levels.
return HitTestResultBehavior.Continue;
}
这篇关于我可以在WPF画布中使用ZIndex较低的HitTest元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!