Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable scroll for WPF Treeview

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.

like image 412
Ronald Avatar asked Aug 21 '09 09:08

Ronald


3 Answers

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!).

like image 193
Kent Boogaart Avatar answered Oct 18 '22 14:10

Kent Boogaart


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.

like image 35
drjeks Avatar answered Oct 18 '22 15:10

drjeks


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>
like image 4
Carlo Avatar answered Oct 18 '22 14:10

Carlo