1

I have a problem that when TreeView is placed inside a container that has ScrollViewer, the TreeView's scrollbars doesn't work, instead the container's ScrollViewer's contents are resized so that all TreeView items are visible.

In WinForms I could just set the container's minimum content width and height, but how to achieve this in WPF?

Here's the example XAML:

<Window x:Class="TestWpfTreeViewInsideScrollViewer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ScrollViewer Name="scroll1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" >
        <Grid MinHeight="230" MinWidth="200" Grid.IsSharedSizeScope="True">
            <Button Content="Button" Width="74" Height="52" Margin="10,24,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Button Content="Button" Height="52" Margin="89,24,10,0" VerticalAlignment="Top"/>
            <Button Content="Button" Width="74" Height="52" Margin="10,81,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>

            <TreeView Margin="10,138,10,55">
                <TreeViewItem IsExpanded="True" Header="This is a long text but the treeview is not scrolling">
                    Instead the TreeView is expanding its size to fit all these nodes inside it, i want that the upper scroller take in only when grid minwidth 200 is reached, not before
                    <TreeViewItem IsExpanded="True" Header="This is a long text but the treeview is not scrolling">This is a long text but the treeview is not scrolling
                    </TreeViewItem>
                </TreeViewItem>
                <TreeViewItem IsExpanded="True" Header="This is a long text but the treeview is not scrolling">This is a long text but the treeview is not scrolling</TreeViewItem>
                <TreeViewItem IsExpanded="True" Header="This is a long text but the treeview is not scrolling">This is a long text but the treeview is not scrolling</TreeViewItem>
            </TreeView>

            <Button Content="Button" Margin="10,0,10,10" Height="40" VerticalAlignment="Bottom"/>
        </Grid>
    </ScrollViewer>

</Grid>

1 Answer 1

5

Scrollviewer has an undefined space for its contents.

This would mean that the TreeView was displayed in a size in which it does not need to display its ScrollViewer.

At least the Width and Height of the TreeView inside the ScrollViewer should be defined.

UPDATE:

If you want to make the Treeview Width resize dynamically, you need to use data binding.

<TreeView Margin="10,138,10,55" Width="{Binding ElementName=scroll1, Path=ActualWidth}">

You can also bind Width to other controls if ScrollViewer does not satisfy the Treeview Width.

I hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, but if I set Height and Width for the Grid, it doesn't resize when I resize the window, how to set the Grid inside the ScrollViewer so that it takes it's size and width from the outer Grid?
Maybe you need to use data binding to set the Width of Treeview This may be not correct but I hope this helps. Width="{Binding ElementName=scroll1, Path=ActualWidth}"
Hey that actualwidth binding works! Thanks! Maybe you want to refine your answer so I can approve it!
The workaround I was using is to bind on other control. If there is no control that would fit what I need then I create additional invisible control. You may put the first 2 buttons within a Grid and bind the with of TreeView to the Grid. This assumes that the grid Left and Right Margin is the same as the TreeView's. Otherwise, it may not give a good result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.