6

I have a Grid inside a ScrollViewer inside a Border inside a StackPanel inside a Window.

The ScrollViewer puts a scrollbar on the right, but it is not scrollable.

How can I get the ScrollViewer to make its contents scrollable?

alt text http://www.deviantsart.com/upload/1bl34e1.png

<Window x:Class="TestScroll234343.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="150" Width="300">
    <StackPanel>
    <!--<StackPanel Height="150"> doesn't work either-->
        <Border>
            <ScrollViewer>            
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Row0"/>
                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Row1"/>
                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Row2"/>
                    <TextBlock Grid.Row="3" Grid.Column="0" Text="Row3"/>
                    <TextBlock Grid.Row="4" Grid.Column="0" Text="Row4"/>
                    <TextBlock Grid.Row="5" Grid.Column="0" Text="Row5"/>
                    <TextBlock Grid.Row="6" Grid.Column="0" Text="Row6"/>
                    <TextBlock Grid.Row="7" Grid.Column="0" Text="Row7"/>
                    <TextBlock Grid.Row="8" Grid.Column="0" Text="Row8"/>
                    <TextBlock Grid.Row="9" Grid.Column="0" Text="Row9"/>
                </Grid>
            </ScrollViewer>
        </Border>

    </StackPanel>
</Window>

1 Answer 1

7

You should set the height of the ScrollViewer.

If you don't, the Border ask the ScrollViewer what height do it want and the ScrollViewer asks the Border what height should it be.

Other options:

  • Change the StackPanel for a DockPanel (it does not grow more than the Window)
  • Set the height of the StackPanel and bind to it in the ScrollViewer

Code:

 <StackPanel Height="140">
    <Border>
        <ScrollViewer Height="{Binding RelativeSource={RelativeSource FindAncestor, 
                    AncestorType={x:Type StackPanel}}, Path=Height}">
Sign up to request clarification or add additional context in comments.

2 Comments

It works if I set the height of the ScrollViewer or the Border, but not the StackPanel or the Window. Is there anyway to tell the ScrollViewer to become the height of the control it is in?
+1, but I also noticed one problem. In my case, I surround ScrollView with ListBox, now the mouse scroll can only scroll along the scroll bar not inside the ListBox. Guess ListBox doesn't relate to the ScrollView. How to allow it scroll on the content as well? (In my original design, I didn't specify the ScrollViewer)

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.