在画布中剪切 WPF 元素
我正在开发一个交互式 WPF 图形/树工具,并将它们之间的节点和链接放置在画布中。节点是用户控件,链接只是线形状,当前链接从一个节点的中心到另一个节点的中心。
当我希望节点稍微透明并且可以看到节点后面的链接时,就会出现问题。 我认为最方便的解决方案是对线条应用剪切或不透明蒙版,这样它们就不会绘制在节点后面,但我一生都无法弄清楚如何?
基本上我无法从节点中找出边界框几何形状来用作线条的剪切几何形状。当然,我也对替代解决方案感兴趣!
I am working on an interactive WPF graph/tree tool and have nodes and links between them placed in a canvas. The nodes are usercontrols and the links are simply Line shapes, and currently the links go from the centre of a node to another node's centre.
The problem arise when I want the nodes to be slightly transparent and one sees the links behind the nodes.
I figured the most convenient solution would be to apply clipping or opacitymask to the lines, so they are not drawn behind the nodes, but I can't for the life of me figure out how?
Basically I can't figure out a bounding box geometry from the nodes to use as a clipping geometry for the lines. I am also interested in alternative solutions, of course!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,你对解决方案的思考太多了。为什么不只是改变链接的逻辑,使线在节点的正确一侧开始/结束,而不是从中心开始???您只需要多做一点数学就能完成此任务。
也就是说,要获取视觉对象的边界框,您可以使用 VisualTreeHelper:: GetContentBounds 帮助器方法。
It would seem to me like you're overthinking the solution. Why not just change the logic for the links so that the lines begin/end at the correct side of the node instead of starting from the center??? You should only need to do a little more math to accomplish this.
That said, to get the bounding box of a Visual you can use the VisualTreeHelper::GetContentBounnds helper method.
VisualTreeHelper.GetContentBounds() 方法似乎每次都返回 Empty。
此问题的替代解决方案可在
在不使用锚点的情况下通过一条线连接两个 WPF 画布元素?
使用边界框来查找交点以绘制线条。
The VisualTreeHelper.GetContentBounds() method seems to return Empty everytime.
An alternative solution to this problem is answered at
Connecting two WPF canvas elements by a line, without using anchors?
that uses bounding boxes to find intersection points to draw the lines from/to.
我们做了类似的事情,我们的解决方案是将链接和节点放在不同的层上。
因此,如果您希望节点出现在链接之上,并且链接的尖端被节点隐藏,您只需更改层的 z 顺序,使节点层位于链接层的前面。
我们使用 VisualHosts 作为图层(您会发现 VisualHost 类 此处)我们的节点和链接对象是 DrawingVisuals。
工作正常,您无需为查找节点的边界等而烦恼。
We worked on something similar and our solution was to put links and nodes on different layers.
So if you want the nodes to appear above the links and the tips of the links to be hidden by the nodes, you just change the z-order of the layers so that the nodes layer is in front of the links-layer.
As layers we used VisualHosts (you find a VisualHost class here) an our node and link objects were DrawingVisuals.
Works fine and you don't need to hassle about finding the borders of your nodes etc.