我正在尝试在 Canvas 上测试一堆 UserControl。我不希望 HitTest() 一直遍历可视化树,所以我使用 FilterCallback 来确保我只对 UserControl 进行命中测试。
我的问题是 UserControl 从来没有命中,它应该,但它没有。如果我使用 FilterCallback,我会返回它没有命中任何内容。如果我让 HitTest 在可视化树中运行,它会跳过 UserControl。
这是一些代码:
<Canvas x:Name="Container">
<UserControl>
<Grid>
<Rectangle />
</Grid>
</UserControl>
<UserControl>
<Grid>
<Rectangle />
</Grid>
</UserControl>
</Canvas>
...
VisualTreeHelper.HitTest(Container, OnFilter, OnResult, myPoint);
...
private void OnResult(DependencyObject o)
{
//I'll get the Rectangle here, but never the userControl
}
private void OnFilter(DependencyObject o)
{
//I will get the UserControl here, but even when I do nothing more than continue, it will not trigger a visualHit. But the child rectangle will.
}