wpf 中的 stackpanel 内平滑滚动

发布于 2024-12-20 23:57:20 字数 551 浏览 1 评论 0原文

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

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

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

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

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

发布评论

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

评论(1

写下不归期 2024-12-27 23:57:20

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

WPF 的 ScrollViewer 尝试一次将整个元素滚动到视图中,这就是您看到跳跃滚动行为的原因。通过将 StackPanel 嵌套在另一个面板中,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>

Wrap your StackPanel in another panel

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.

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