如何让我的ScrollViewer滚动视图区域?

时间:2009-12-03 15:59:01

标签: wpf xaml grid scrollviewer

窗口内 StackPanel 内的边框内的 ScrollViewer 内有网格

ScrollViewer在右侧放置滚动条,但不可滚动

如何让ScrollViewer使其内容可滚动?

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 个答案:

答案 0 :(得分:7)

您应该设置ScrollViewer的高度。

如果不这样做,Border会询问ScrollViewer它想要的高度,而ScrollViewer会询问Border它的高度。

其他选择:

  • 更改StackPanel的{​​{1}}(增长速度不超过DockPanel
  • 设置Window的高度并在StackPanel
  • 中绑定它

代码:

ScrollViewer