1

I am working with VisualCollection, Visual and HitTest in WPF and encountered a problem.

I tried to make a custom visual drawing as follows:

public class MyDrawing : Visual
{
    VisualCollection vc;

    public MyDrawing()
    {
        vc = new VisualCollection(this);
    }

     // ...
     DrawingVisual rectangle = new DrawingVisual();
     // ...

     vc.Add(rectangle);
}

public class DrawingArea : FrameworkElement
{
    VisualCollection vc;

    public DrawingArea()
    {
        vc = new VisualCollection(this);

        MyDrawing md1 = new MyDrawing();

        vc.Add(md1);
    }    

    public void TryToHit(Point p)
    {      
        HitTestResult result = VisualTreeHelper.HitTest(this, p);
    }    
}

Then I found that the result is rectangle but not md1.

How could I make MyDrawing become the basic Visual element so that the VisualTreeHelper would not further do HitTest inside?

Thank you very much.

Mualig
  • 1,444
  • 1
  • 19
  • 42
user1184598
  • 449
  • 1
  • 5
  • 11
  • 2
    See [Hit Testing in the Visual Layer](http://msdn.microsoft.com/en-us/library/ms752097.aspx), especially the section [Using a Hit Test Filter Callback](http://msdn.microsoft.com/en-us/library/ms752097.aspx#using_a_hit_test_filter_callback). – Clemens Jun 27 '12 at 07:34
  • Add a specific example of what you have tried. – gliderkite Jun 27 '12 at 08:16

1 Answers1

0

Instead of Drawingvisual. Try the below one.

  public class NoHitTestDrawingVisual : DrawingVisual
    {

        protected override GeometryHitTestResult HitTestCore(GeometryHitTestParameters hitTestParameters)
        {
            return null;
        }

        protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
        {
            return null;
        }

    }
Sivakumar
  • 478
  • 2
  • 13
  • I'm afraid this won't work unless the parent visual (`MyDrawing md1` here) has itself rendered (and hence hit-testable) content. – Clemens Jun 27 '12 at 08:57