问题
So I have drawn some lines on a Visual Layer
and added it to Canvas
. Now that I'm trying to HitTest them I'm getting the whole visual. How can I get only the line that I click on?
public class MyDrawing : DrawingVisual
{
public MyDrawing ()
{
using (var dc = RenderOpen())
{
foreach (var block in blocks)
{
// Draw lines here
}
}
}
}
private void OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
var x = MousePos.RightDown.X;
var y = MousePos.RightDown.Y;
var hitRect = new Rect(x - 2, y - 2, 4, 4);
VisualTreeHelper.HitTest(MyCanvas, null, MyCallback,
new GeometryHitTestParameters(new RectangleGeometry(hitRect)));
VisualTreeHelper.HitTest(zones, null, MyCallback,
new GeometryHitTestParameters(new RectangleGeometry(hitRect)));
}
private static HitTestResultBehavior MyCallback(HitTestResult result)
{
// I want to get the line I have hit here
return HitTestResultBehavior.Stop;
}
来源:https://stackoverflow.com/questions/25754861/get-the-hittesting-result-when-lots-of-drawings-are-in-one-single-visual