3

I have a TreeView (the only control) inside a Grid, and I only want to see the vertical scrollbar when the height isn't enough.

To not have the horizontal scrollbar, I have to account for its width to the width of the TreeView, right? But when I only specify the width to stretch the TreeView's width to the width of the Window, no scrollbar appears.

Is there a way to solve this?

Alternatively if I can make it so that I don't have to specify the width and height of the TreeView and it resizes its width when the height isn't enough, to fit the vertical scrollbar, that would be the best.

Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • Maybe my solution to similar problem will help: [Prevent Automatic Horizontal Scroll in TreeView](http://stackoverflow.com/questions/3225940/prevent-automatic-horizontal-scroll-in-treeview/9479175#9479175) – Dariusz Wasacz Feb 28 '12 at 09:18

1 Answers1

20

I use these attributes on my TreeView's ScrollViewer:

<TreeView ScrollViewer.VerticalScrollBarVisibility="Auto"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled" />

As long as the width of your TreeView's content stretches to just fit the width of your TreeView, you should be fine.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Thanks it did work great. Out of curiosity, why did you say "just in case"? :O Also in your last sentence, you mean having the TreeView width to be as large as the Window's width, minus the scrollbar width that will appear later? – Joan Venge Apr 20 '11 at 21:37
  • 2
    @Joan Venge: No idea why I put "just in case" there... -erases- By my last sentence, I mean having the width of the content inside the TreeView fit the TreeView's own width. – BoltClock Apr 20 '11 at 21:39
  • Thanks man, got it now. Will mark it as answer when the timer allows me. – Joan Venge Apr 20 '11 at 21:40
  • Btw I think I got why you wrote "just in case". I think you meant, you use these properties all the time in your TreeView controls, so you set them like that, just in case you might need them later. Anyway this is my guess. Thanks again. – Joan Venge Apr 20 '11 at 21:45
  • I had my TreeView wrapped in a StackPanel. After adding this the scroll bars still didn't appear, replacing the stackpanel with a grid and giving the specific row the TreeView was on a height of * + this code solved it for me – MaxJ Oct 12 '17 at 15:15