views:

2061

answers:

3

Can anybody help me out with how to enable a treeview to scroll? There must be a simple way but I can't make it work in my code. After multiple failed tries, I currently have something like this:

        <ScrollViewer CanContentScroll="True">
           <TreeView ...>
           </TreeView>
        </ScrollViewer>

I do see an 'disabled' scrollbar, but when the notes of the treeview are larger than the screen height, no scrolling is activated.

+2  A: 

The TreeView control itself includes a ScrollViewer in its template. You should be able to just use a TreeView inside an appropriate host (not a StackPanel!).

HTH, Kent

Kent Boogaart
What exactly is an appropriate host? My TreeView DOES lay inside a StackPanel though.
Littlefool
I think he's talking about the ScrollViewer not being the appropiate host, as for the "not a StackPanel!" part, I don't really get it, there should be no difference in the TreeView's behavior if you use it in a Grid, StackPanel, WrapPanel, UniformGrid, etc.
Carlo
A `StackPanel` gives its content whatever width (when orientation is horizontal) or height (when orientation is vertical) its children ask for. Thus, if you put a `TreeView` (or `ListBox`, or whatever) in a `StackPanel`, then the `TreeView` will think that it has sufficient height to display all items without the need for a `ScrollBar`. In fact, the `TreeView` will be cut off because you'll run out of screen real-estate. Use a `Grid` instead.
Kent Boogaart
+1  A: 

Do you have a height explicitly set on your window? If you want to see the scrollbar something must define the height of the TreeView or its container, otherwise it won't know when it needs to show the scrollbar.

Example:

<Window x:Class="StackOverflowTests.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" x:Name="window1" Height="300" Width="300">
    <Grid>
     <TreeView  Name="treeView1" Height="150" VerticalAlignment="Top">
      <TreeViewItem Header="Root" IsExpanded="True">
       <TreeViewItem Header="Item 1"></TreeViewItem>
       <TreeViewItem Header="Item 2"></TreeViewItem>
       <TreeViewItem Header="Item 3"></TreeViewItem>
       <TreeViewItem Header="Item 4"></TreeViewItem>
       <TreeViewItem Header="Item 5"></TreeViewItem>
       <TreeViewItem Header="Item 6"></TreeViewItem>
       <TreeViewItem Header="Item 7"></TreeViewItem>
       <TreeViewItem Header="Item 8"></TreeViewItem>
       <TreeViewItem Header="Item 9"></TreeViewItem>
       <TreeViewItem Header="Item 10"></TreeViewItem>
       <TreeViewItem Header="Item 11"></TreeViewItem>
       <TreeViewItem Header="Item 12"></TreeViewItem>
       <TreeViewItem Header="Item 13"></TreeViewItem>
       <TreeViewItem Header="Item 14"></TreeViewItem>
       <TreeViewItem Header="Item 15"></TreeViewItem>
       <TreeViewItem Header="Item 16"></TreeViewItem>
       <TreeViewItem Header="Item 17"></TreeViewItem>
       <TreeViewItem Header="Item 18"></TreeViewItem>
       <TreeViewItem Header="Item 19"></TreeViewItem>
       <TreeViewItem Header="Item 20"></TreeViewItem>
       <TreeViewItem Header="Item 21"></TreeViewItem>
       <TreeViewItem Header="Item 22"></TreeViewItem>
       <TreeViewItem Header="Item 23"></TreeViewItem>
       <TreeViewItem Header="Item 24"></TreeViewItem>
       <TreeViewItem Header="Item 24"></TreeViewItem>
      </TreeViewItem>
     </TreeView>
    </Grid>
</Window>
Carlo
Sorry, tried to add heights to the treeview and its container but without success
Littlefool
Hmmm I did get the scroll bar, I'll add my example to my answer, see if it works, basically the window's height is set to 300 and the treeview's height is set to 150, so the treeview is half as tall as the window and it gets a scroll bar if its items overflow the height.
Carlo
I added a height to the grid and this worked.
Anthony Potts
+1  A: 

The TreeView contains a ScrollViewer, but as @Carlo said, the TreeView or its container needs to have a height. Alternatively, the TreeView should be hosted in a container that doesn't give infinite height to its children (i.e. a StackPanel which I think was what @Kent was meaning). So place it inside a Grid, no need to give the Grid or the TreeView an explicit height and you should get the scrollbars.

drjeks