简体   繁体   English

在wpf中的stackpanel内平滑滚动

[英]Smooth scroll within stackpanel in wpf

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

This is the code for the stackpanel which is filled in runtime with multiple WrapPanels. 这是stackpanel的代码,它在运行时用多个WrapPanel填充。 Scroll Viewer scrolls through the panels - one at a time - which makes it really inconvenient because all panels are of different sizes. 滚动查看器滚动面板 - 一次一个 - 这使得它非常不方便,因为所有面板都有不同的尺寸。 I tried this one by setting ScrollViewer.CanContentScroll="False" property in StackPanel while deleting it in ScrollViewer, didn't help - scroll bar disappeared at all. 我想这一个在StackPanel中设置ScrollViewer.CanContentScroll =“假”的属性,而在ScrollViewer中删除,并没有帮助-滚动条消失的。 What's the solution for smooth scroll bar? 平滑滚动条的解决方案是什么?

Wrap your StackPanel in another panel StackPanel包装在另一个面板中

WPF's ScrollViewer tries to scroll entire elements into view at a time, which is why you see the jumpy scroll behavior. WPF的ScrollViewer尝试一次将整个元素滚动到视图中,这就是您看到跳转滚动行为的原因。 By nesting the StackPanel in another Panel, the ScrollViewer will try and scroll the entire StackPanel into view, which is too big so it will use smooth scrolling. 通过将StackPanel嵌套在另一个Panel中,ScrollViewer将尝试将整个StackPanel滚动到视图中,该视图太大,因此它将使用平滑滚动。

Here's an example - Removing the DockPanel will give you a jumpy scroll, but with it you'll get smooth scrolling behavior 这是一个例子 - 删除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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM