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.
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
!).
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With