如何让 ScrollViewer 滚动视图区域?

发布于 2024-08-13 12:33:05 字数 2651 浏览 3 评论 0原文

我在 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾听心声的旋律 2024-08-20 12:33:05

您应该设置 ScrollViewer 的高度。

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

其他选项:

  • StackPanel 更改为 DockPanel(它的增长不会超过 Window
  • 设置 StackPanel< 的高度/code> 并在 ScrollViewer 代码中绑定到它

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

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}">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文