2

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.

GraphicsBase 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");
Alp
  • 553
  • 11
  • 30
  • can you share a code that calls `MyHitTestFilter` ? – TarasB Dec 09 '10 at 23:06
  • What BackgroundLine is derived from? – rooks Dec 10 '10 at 08:15
  • Both BackgroundLine and GraphicsBase are derived from DrawingVisual. drawingCanvas contains objects of these 2 classes only. – Alp Dec 10 '10 at 08:38
  • It's maybe the problem in the way you add visuals to the canvas, because I get proper types in both HitTestFilterCallback and HitTestResultCallback. – rooks Dec 10 '10 at 12:56

1 Answers1

0

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.

Alp
  • 553
  • 11
  • 30