Chr*_*col 4 wpf visualtreehelper hittest
我正试图在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)
Ale*_*ven 11
我知道回答这个问题已经很晚了但是接下来是:另一种方法是在UserControl上覆盖HitTestCore并为其提供从中预期的默认行为:
protected override System.Windows.Media.HitTestResult HitTestCore(System.Windows.Media.PointHitTestParameters hitTestParameters)
{
return new PointHitTestResult(this, hitTestParameters.HitPoint);
}
Run Code Online (Sandbox Code Playgroud)
(当然你可以复杂化并测试实际的孩子或他们的边界框的组合,但对我来说,用户控件的边界框已经足够了;而且,如果你想针对几何体进行测试,你需要覆盖第二次重载.)
这使它按预期工作,HitTestFilterBehavior.ContinueSkipChildren
在过滤器中使用时过滤掉子项.
归档时间: |
|
查看次数: |
8883 次 |
最近记录: |