views:

449

answers:

2

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!

+1  A: 

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.

Drew Marsh
Yes, I thought of that but there are two reasons I don't like that approach.1) I am using panning and zooming in the canvas, and also the node sizes vary depending on content and per-node scaling transformations. I just find it hard to figure out the exact dimensions of the nodes in canvas coordinates. I know it's doable, I'm just struggling a bit.2) Even if I knew the exact bounds of the nodes, drawing a thick line to join the bounds would produce artifacts if the line and node edges were not perpendicular. If I instead clip the line by the node geometry it would join them seamlessly.
Andreas Larsen
Fair enough, I can tell you how to get the bounding box. I will update my answer.
Drew Marsh
Thanks, that is exactly what I was looking for!
Andreas Larsen
A: 

The VisualTreeHelper.GetContentBounds() method seems to return Empty everytime.

An alternative solution to this problem is answered at

http://stackoverflow.com/questions/1737509/connecting-two-wpf-canvas-elements-by-a-line-without-using-anchors/1737536#1737536

that uses bounding boxes to find intersection points to draw the lines from/to.

Andreas Larsen