6

我在Window内的StackPanel内的Border内的ScrollViewer内有一个Grid

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

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

替代文字 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>
4

1 回答 1

7

你应该设置的高度ScrollViewer

如果你不这样做,Border询问ScrollViewer它想要什么高度,ScrollViewer询问Border它应该是什么高度。

其他选项:

  • 改变StackPanela DockPanel(它不会比 增长更多Window
  • 设置的高度StackPanel和绑定到它ScrollViewer

代码:

 <StackPanel Height="140">
    <Border>
        <ScrollViewer Height="{Binding RelativeSource={RelativeSource FindAncestor, 
                    AncestorType={x:Type StackPanel}}, Path=Height}">
于 2009-12-03T16:04:07.883 回答