我正试图在Canvas上测试一堆UserControls.我不希望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.
}
Run Code Online (Sandbox Code Playgroud)