0

I'd like to spruce up the WPF TreeView and add some functionality to it that needs to draw lines between the items of the TreeView. For that I created my own control inheriting from TreeView and drawing the lines in the OnRender override of that control:

Public Class MyTreeView
    Inherits TreeView

    Protected Overrides Sub OnRender(drawingContext As DrawingContext)
        MyBase.OnRender(drawingContext)

        drawingContext.DrawLine(New Pen(Brushes.Black, 1), New Point(0, 0), New Point(Me.ActualWidth, Me.ActualHeight))
    End Sub

End Class

But I can only see my drawings in the running program, when I set the Background of my TreeView to Transparent.

<local:MyTreeView Background="Transparent" />

As soon as I put a white or gradient background to my TreeView it hides my drawings.

Is it possible to add some drawings in the OnRender override, that is rendered on top of the given Background?

For reasons that belong to another question I can't work with a transparent background and I can't work with a Border with background under a transparent TreeView simulating the background in the TreeView.

XAMlMAX
  • 2,268
  • 1
  • 14
  • 23
Nostromo
  • 1,177
  • 10
  • 28
  • Have you tried using Blend and get the style for the tree view? Then just modify that style with your lines. `OnRender` sounds a bit hacky (like WinForms) – XAMlMAX Dec 03 '19 at 14:16
  • Since the size and amount of items in the `TreeView` isn't fixed I can't draw fixed lines in a `Style`, the count and position of the lines are dependent on the size and position of the items. – Nostromo Dec 04 '19 at 05:59
  • Are you trying to tell me that the Tree View isn't designed to have indeterminate number of items? In WPF use styles, it will save you headache later in the development. WinForms is a thing of a past, just like stone tablets and chisels. – XAMlMAX Dec 04 '19 at 09:46
  • How should I put lines in a style, when I don't know how many lines and from where to where to draw the lines? Maybe you can enlighten me with a short example? – Nostromo Dec 05 '19 at 09:39
  • First [SO post](https://stackoverflow.com/a/19560467/2029607) in my google search. It is very comprehensive but if you need help, let us know. – XAMlMAX Dec 05 '19 at 13:08
  • So you want to put the lines into the items, not into the TreeView itself. Have to have a look in this. My items are arranged horizontally, not vertically, but I'll give it a try. – Nostromo Dec 06 '19 at 09:25

0 Answers0