【问题标题】:How can I get my ScrollViewer to scroll the view area?如何让我的 ScrollViewer 滚动查看区域?
【发布时间】:2009-12-03 15:59:00
【问题描述】:

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

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>

【问题讨论】:

    标签: wpf xaml grid scrollviewer


    【解决方案1】:

    你应该设置ScrollViewer的高度。

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

    其他选项:

    • StackPanel 更改为DockPanel(它不会比Window 增长更多)
    • 设置StackPanel的高度并绑定到ScrollViewer

    代码:

     <StackPanel Height="140">
        <Border>
            <ScrollViewer Height="{Binding RelativeSource={RelativeSource FindAncestor, 
                        AncestorType={x:Type StackPanel}}, Path=Height}">
    

    【讨论】:

    • 如果我设置 ScrollViewer 或 Border 的高度,但不设置 StackPanel 或 Window 的高度,它会起作用。有没有办法告诉 ScrollViewer 成为它所在控件的高度?
    • +1,但我也注意到一个问题。在我的例子中,我用 ListBox 包围了 ScrollView,现在鼠标滚动只能沿着滚动条滚动,而不是在 ListBox 内。猜测 ListBox 与 ScrollView 无关。如何允许它在内容上滚动? (在我的原始设计中,我没有指定 ScrollViewer)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    相关资源
    最近更新 更多