Search code examples
wpfcanvaslineclippingbounding-box

Clipping WPF elements in a canvas


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!


Solution

  • 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.