MSAGL中设置节点位置后失效的问题求助
MSAGL WPF示例中手动设置节点位置失效的解决办法
问题根源在于:给graphViewer重新赋值Graph后,MSAGL默认会触发自动布局算法,重新计算并覆盖你手动设置的节点坐标。
以下是几种可行的解决方式:
关闭自动布局功能
在操作节点前先禁用自动布局,避免后续布局计算重置位置:graphViewer.AutoLayout = false; Graph graph = graphViewer.Graph; graph.AddNode("2"); Microsoft.Msagl.Core.Geometry.Point position = new Microsoft.Msagl.Core.Geometry.Point(199, 991); graph.FindNode("2").GeometryNode.Center = position; // 无需重新赋值Graph,直接刷新视图 graphViewer.InvalidateVisual();避免重复赋值Graph
既然已经获取了graphViewer.Graph的引用,修改节点属性后不需要再次将graph赋值回graphViewer,直接调用刷新方法即可生效:Graph graph = graphViewer.Graph; graph.AddNode("2"); Microsoft.Msagl.Core.Geometry.Point position = new Microsoft.Msagl.Core.Geometry.Point(199, 991); graph.FindNode("2").GeometryNode.Center = position; // 刷新视图让修改生效 graphViewer.InvalidateVisual();若必须重新赋值Graph
赋值完成后再次手动设置节点位置,并确保自动布局未开启:graphViewer.AutoLayout = false; Graph graph = graphViewer.Graph; graph.AddNode("2"); graphViewer.Graph = graph; // 重新赋值后再次设置位置 Microsoft.Msagl.Core.Geometry.Point position = new Microsoft.Msagl.Core.Geometry.Point(199, 991); graphViewer.Graph.FindNode("2").GeometryNode.Center = position; graphViewer.InvalidateVisual();
内容的提问来源于stack exchange,提问作者Burgha
相关产品推荐
相关产品推荐

