1

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.

Bin4ry
  • 652
  • 9
  • 34
  • Does it work properly? Could you maybe share your solution? – Zoli Mar 14 '22 at 13:39
  • 1
    Here is the control code: https://pastebin.com/Y0qDggb3 and the part of the XAML: https://pastebin.com/PYyD2ZLR you may call .ReRender(); to apply changes. Hope it helps – Bin4ry Mar 14 '22 at 17:54
  • Thanks, going to have a look... – Zoli Mar 16 '22 at 15:10
  • Ok, I had a look, and it **does not work** for me. The lines are drawn, but the TreeView and its items "overwrite" it, so it's not visible. If I just duplicate the draw of the lines e.g.: horizontally shifted, then I can see it. [link](https://i.ibb.co/NTX9nXH/tree.png) Here I duplicate the drawing +250 pixels to the right. Clearly visible that the TreeView "overdraws" the original lines. Here is the App: [link](https://filetransfer.io/data-package/r6xm11he#link) – Zoli Mar 16 '22 at 17:29
  • You need to add a style as well. Here is mine for the same look and feel as posted in the original question. https://pastebin.com/RtygZhd8 Edit: If I adjust your VM population it looks like this: https://ibb.co/sCxQ6Th Hope that helps. – Bin4ry Mar 17 '22 at 08:18
  • Yes, I just realized if I set background to transparent, it's visible – Zoli Mar 17 '22 at 09:31

1 Answers1

1

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

Rekshino
  • 6,954
  • 2
  • 19
  • 44