开发者

WPF - Hit Test Filter Callback

开发者 https://www.devze.com 2023-01-30 08:12 出处:网络
I have a canvas and it has child DrawingVisuals in its VisualCollection. I want to hit test against some type of child but not for others. To do that I wroteHitTestFilterCallback function:

I have a canvas and it has child DrawingVisuals in its VisualCollection. I want to hit test against some type of child but not for others. To do that I wrote HitTestFilterCallback function:

public HitTestFilterBehavior MyHitTestFilter(DependencyObject o)
{
    Debug.WriteLine(o.GetType());
    if (o is BackgroundLine)
    {
        return HitTestFilterBehavior.ContinueSkipSelf;
    }
    else
    {
        return HitTestFilterBehavior.Continue;
    }
}

So I check whether the child of canvas is a BackgroundLine, which is derived from DrawingVisual, and if it is I skip it. However, the type I am getting from Debug.WriteLine(o.GetType()) is only System.Windows.Media.DrawingVisual. Is there a way I can find the most specific object type?

Rest of the code is below. I want to test against GraphicsBase objects only.

Gra开发者_Python百科phicsBase hit = null;
public HitTestResultBehavior MyHitTestResult(HitTestResult result)
{
    hit = (GraphicsBase)result.VisualHit;

    return HitTestResultBehavior.Stop;
}

    VisualTreeHelper.HitTest(drawingCanvas, new HitTestFilterCallback(MyHitTestFilter), 
new HitTestResultCallback(MyHitTestResult), new PointHitTestParameters(point));

if (hit != null)
    Debug.WriteLine("hit");
else
    Debug.WriteLine("nothing");


I found the problem. The DrawingVisual object I am seeing was the rectangle I added for background color. I forgot about that and thought I was getting BackgroundLine object's type as DrawingVisual. I can get the specific BackgroundLine type as rooks said. Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消