带有 VirtualizingStackPanel 的 ItemsControl 禁用 ScrollViewer 中的水平动画 [英] ItemsControl with VirtualizingStackPanel disables horizontal animations in ScrollViewer

查看:31
本文介绍了带有 VirtualizingStackPanel 的 ItemsControl 禁用 ScrollViewer 中的水平动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 UWP 应用构建自定义 XAML 控件,该应用严重依赖带有捕捉点的 ScrollViewer.

I'm building a custom XAML control for a UWP app that relies heavily on a ScrollViewer with snap points.

我非常希望将绑定到控件的内容虚拟化,因此我使用了 ItemsControl.但是,当我在ItemsControl中使用VirtualizingStackPanel,然后将ScrollViewer上的ChangeView()调用到特定的Horizo​​ntalOffset时,滚动到新偏移量时的动画效果被禁用(它只是直接跳转到偏移量).如果我只是将 VirtualizingStackPanel 替换为 StackPanel(无虚拟化),则水平动画会起作用.

I would really like the content that is bound to the control to be virtualized, so I'm using an ItemsControl. However, when I use a VirtualizingStackPanel in the ItemsControl, and then call ChangeView() on the ScrollViewer to a specific HorizontalOffset, the animation effect when scrolling to the new offset is disabled (it just jumps directly to the offset). If I simply replace the VirtualizingStackPanel with a StackPanel (no virtualization), the horizontal animations work.

问题:有谁知道如何在更改偏移量时使用 VirtualizingStackPanel 启用水平动画?

Question: Does anyone know how to use a VirtualizingStackPanel and enable horizontal animations when changing the offset?

这是调整水平偏移的 C#(customScrollViewer 是通过爬树访问的,因为它是 ControlTemplate 样式的一部分):

Here is the C# adjusting the horizontal offset (the customScrollViewer is being accessed via tree-crawling, since it is part of the ControlTemplate style):

customScrollViewer.ChangeView(500, null, null, false);

这是 ItemsControl 的 XAML 样式:

And here is the XAML style for the ItemsControl:

       <Style x:Key="ItemsControlSnapStyle" TargetType="ItemsControl">
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ItemsControl">
                    <ScrollViewer
                        x:Name="customScrollViewer"
                        VerticalScrollBarVisibility="Disabled" 
                        HorizontalScrollBarVisibility="Auto" 
                        HorizontalSnapPointsType="Mandatory">
                        <ItemsPresenter />
                    </ScrollViewer>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

谢谢!

推荐答案

很好的问题,我可以重现这种行为,而且它看起来很符合设计.源自 VirtualizingStackPanel 文件.它只能用于显示 ItemsControl 中的项目. 一般情况下.在备注部分, 从 Windows 8.1 开始,ItemsStackPanel 被用作 ListView 的默认 ItemsPanel.如果您修改了 ItemsPanel,我们建议您使用 ItemsStackPanel 或 ItemsWrapGrid 而不是 VirtualizingStackPanel 或 WrapGrid.

Great question, I could reproduce this behavior, and it looks by-design. Derive from VirtualizingStackPanel document. It can only be used to display items in an ItemsControl. Generally. At the remarks part, Starting in Windows 8.1, ItemsStackPanel is used as the default ItemsPanel for ListView. If you modify the ItemsPanel, we recommend you use ItemsStackPanel or ItemsWrapGrid instead of VirtualizingStackPanel or WrapGrid.

如果我们将 VirtualizingStackPanel 替换为 ItemsStackPanel,并且 ChangeView 方法可以处理动画.而ItemsStackPanel 也支持virtualizes.因此,我们建议您可以使用 ItemsStackPanel 作为 ItemsControl 的 ItemsPanel.

If we replace VirtualizingStackPanel with ItemsStackPanel, and ChangeView method could work with animation. And ItemsStackPanel also support virtualizes. So we suggest you could use ItemsStackPanel as ItemsPanel for the ItemsControl.

<Setter Property="ItemsPanel">
    <Setter.Value>
        <ItemsPanelTemplate>
            <ItemsStackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </Setter.Value>
</Setter>

这篇关于带有 VirtualizingStackPanel 的 ItemsControl 禁用 ScrollViewer 中的水平动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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