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.