在 wpf 中的堆栈面板内平滑滚动 [英] Smooth scroll within stackpanel in wpf

查看:63
本文介绍了在 wpf 中的堆栈面板内平滑滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是 stackpanel 的代码,它在运行时填充了多个 WrapPanel.Scroll Viewer 滚动浏览面板 - 一次一个 - 这真的很不方便,因为所有面板的大小都不同.我通过设置尝试了这个 one在 ScrollViewer 中删除 StackPanel 中的 ScrollViewer.CanContentScroll="False" 属性并没有帮助 - 滚动条完全消失了.平滑滚动条的解决方案是什么?

This is the code for the stackpanel which is filled in runtime with multiple WrapPanels. 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. What's the solution for smooth scroll bar?

推荐答案

将您的 StackPanel 包裹在另一个面板中

Wrap your StackPanel in another panel

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

WPF's ScrollViewer tries to scroll entire elements into view at a time, which is why you see the jumpy scroll behavior. 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.

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

Here's an example - Removing the DockPanel will give you a jumpy scroll, but with it you'll get smooth scrolling behavior

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

这篇关于在 wpf 中的堆栈面板内平滑滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆