Problem with VisualTreeHelper.HitTest in WPF

后端 未结 2 505
闹比i
闹比i 2020-12-11 20:29

I\'m trying to hit-test a bunch of UserControls on a Canvas. I don\'t want the HitTest() to walk the whole way through the visual tree, so I\'m using the FilterCallback to

2条回答
  •  旧巷少年郎
    2020-12-11 20:59

    I know it's pretty damn late to answer this but here goes: a different approach is to override HitTestCore on the UserControl and provide it with the default behavior that would be expected from it:

    protected override System.Windows.Media.HitTestResult HitTestCore(System.Windows.Media.PointHitTestParameters hitTestParameters)
    {
        return new PointHitTestResult(this, hitTestParameters.HitPoint);
    }
    

    (of course you could complicate things and hit-test the actual children or the combination of their bounding boxes, but for me the bounding box of the user control was good enough; also, if you want to hit-test against geometry you need to override that second overload as well.)

    This makes it work as expected, filtering out the children when using HitTestFilterBehavior.ContinueSkipChildren in the filter.

提交回复
热议问题