Search code examples
c#wpftreeviewdrawingcontext

WPF TreeView Remove drawn lines (ReDraw)


Because a normal TreeView doesn't fit my needs, I created my own TreeView, inherit from TreeView and Draw lines between my TreeViewItems. Something like this enter image description here

So far so good, but I would like to ReDraw (Remove add lines) after the tree has been built and drawn. Currently I do everything in the OnRender method, which already provides the DrawingContext to draw lines.

            //Point connections from the parent to the childs.
            Point parentStart = parentCenter;
            Point parentEnd = new Point(parentCenter.X, middleParentChild);
            Point childEnd = new Point(childCenter.X, middleParentChild);
            Point childStart = childCenter;

            drawingContext.DrawLine(Pen, parentStart, parentEnd);
            drawingContext.DrawLine(Pen, parentEnd, childEnd);
            drawingContext.DrawLine(Pen, childEnd, childStart);

            //recursivly do this for all children
            DrawConnections(Pen, drawingContext, item);

But I have no access to DrawingContext after the control has been rendered once. Saved in a lokal variable, I am not able to remove already drawn shapes nor redraw anything, because the DrawingContext is already disposed.


Solution

  • You can use YourTreeView.InvalidateVisual() to redraw your tree.