简体   繁体   中英

how to display a scrollable treeview control on wpf form

how to render a scrollable wpf treeview control on a window that occupies the left corner of the window and adjusts with the height of the window. I have been experimenting with Grid and dockpanel but the scrollbar does not appear unless you specify the height. please answer with a XAML markup.

Tell me if this is what you need:

<Window x:Class="WpfApplication6.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Name="window" Height="350" Width="525">
<Grid>
    <TreeView 
        ItemsSource="{Binding ElementName=window, Path=TreeviewDummySource}" 
        Background="Gray" 
        Width="150" 
        HorizontalAlignment="Left"
        ScrollViewer.VerticalScrollBarVisibility="Visible"/>
</Grid>
</Window>

Dummy ItemSource:

    public int[] TreeviewDummySource
    {
        get { return treeviewDummySource; }
    }

    private int[] treeviewDummySource = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 };

So the trick is that the ScrollBar will appear only if the content of the ScrollViewer is larger than the available size. By specifying the ScrollViewer.VerticalScrollBarVisibility="Visible" in TreeView properties the scrollviewer will always be present (enabled or disabled). Try changing the height of the window to see how the ScrolBar gets enabled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM