在wpf中的stackpanel内平滑滚动

时间:2011-12-13 12:45:34

标签: wpf scroll stackpanel

<ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="True">
                <StackPanel Name="basePanel" Orientation="Vertical" Height="450" />
            </ScrollViewer>

这是stackpanel的代码,它在运行时用多个WrapPanel填充。滚动查看器滚动面板 - 一次一个 - 这使得它非常不方便,因为所有面板都有不同的尺寸。我通过在StackPanel中设置ScrollViewer.CanContentScroll =“False”属性来尝试此one,同时在ScrollViewer中删除它,没有帮助 - 滚动条完全消失了。平滑滚动条的解决方案是什么?

1 个答案:

答案 0 :(得分:14)

将您的StackPanel换入另一个小组

WPF的ScrollViewer尝试一次将整个元素滚动到视图中,这就是您看到跳转滚动行为的原因。通过将StackPanel嵌套在另一个Panel中,ScrollViewer将尝试将整个StackPanel滚动到视图中,该视图太大,因此它将使用平滑滚动。

这是一个例子 - 删除DockPanel会给你一个跳跃的滚动,但有了它你会得到平滑的滚动行为

<ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="True" Height="250">
    <DockPanel>
        <StackPanel Name="basePanel" Orientation="Vertical" Width="200">
            <Rectangle Height="75" Fill="Red" Width="200" />
            <Rectangle Height="50" Fill="Orange" Width="200" />
            <Rectangle Height="75" Fill="Yellow" Width="200" />
            <Rectangle Height="75" Fill="Green" Width="200" />
            <Rectangle Height="75" Fill="Black" Width="200"  />
            <Rectangle Height="75" Fill="Purple" Width="200" />
        </StackPanel>
    </DockPanel>
</ScrollViewer>