删除路径中的特定LineGeometry

我的应用程序包含一个带有一些用户可拖放UI元素的canvas,可以使用行进一步连接。

为了连接两个UIelements,我使用了一个包含GeometryGroup的Path,它还包含LineGeometry作为其子元素。 检查此屏幕截图 。

因此,如图所示,这三个项目通过包含2个LineGeometry的Path连接。 我正在尝试实现“删除链接”选项,但我所能做的就是删除整个路径,这将删除LineGeometry。 我如何具体选择特定的线段并将其删除?

没有为命中测试实现数学的解决方案。 例如,如果要通过鼠标按下删除行:

  

Canvas_MouseDown应该像这样实现:

 private void Canvas_MouseDown(object sender, MouseButtonEventArgs e) { var canvas = sender as Canvas; if (canvas == null) return; // 1. Find a Path containing links HitTestResult hitTestResult = VisualTreeHelper.HitTest(canvas, e.GetPosition(canvas)); var path = hitTestResult.VisualHit as Path; if (path == null) return; // 2. Iterate through geometries of the Path and hit test each one // to find a line to delete var geometryGroup = path.Data as GeometryGroup; if (geometryGroup == null) return; GeometryCollection geometries = geometryGroup.Children; Point point = e.GetPosition(path); var pen = new Pen(path.Stroke, path.StrokeThickness); var lineToDelete = geometries.OfType() .FirstOrDefault(l => l.StrokeContains(pen, point)); // 3. Delete link if (lineToDelete != null) geometries.Remove(lineToDelete); } 

您需要到点击点的距离。

 public void RemoveLink(Point point) { // ... // point - 2D click point // lineList - list of links (lines) setDistance(lineList, point) lineList.Sort(compare); lineList[0].remove(); // ... } private static Comparison compare = new Comparison(GeomObject.CompareByDistance); public static int CompareByDistance(GeomObject go1, GeomObject go2) { return go1.mDistance.CompareTo(go2.mDistance); } private void setDistance(List lineList, Point point) { // set mDistance for each Line // mDistance - distance to point }