c# - 通过动画在 WPF ItemsControl 或 ScrollViewer 中循环项目?

标签 c# wpf user-controls wpf-controls itemscontrol

当在 ItemsControlScrollViewer 中移动项目时,我能否循环内容以便第一个项目出现在最后一个项目“之后”(如用户所感知的那样) )?

我有一个自定义的 ScrollViewer,它将在选择之间设置动画,目前包含一个 ItemControl。用于设置我的控件的 XAML 如下:

<local:AnimatedScrollControl x:Name="myScroll" Grid.Column="1" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden">
    <ItemsControl x:Name="myList"
                    ItemsSource="{Binding SlidesList}"
                    ItemTemplate="{StaticResource SlideTemplateOne}"
                    VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</local:AnimatedScrollControl>

ItemControl 中的每一项都占据控件的整个可见区域,因此用户一次只能看到一项。下图表示正在运行的控件,红色框表示视口(viewport)。计时器会自动移动到下一个项目。 Control Example 01 移动幻灯片的代码(为简洁起见进行了简化)目前如下所示:

private void NextSlide()
{
    _currentSlide++;

    // get the current offset
    double offset = (double)myScroll.GetValue(AnimatedScrollControl.SlideOffsetProperty);

    // get the width of the current slide
    FrameworkElement elementContainer = myList.ItemContainerGenerator.ContainerFromItem(CurrentSlide) as FrameworkElement;

    // set the `to` value for a single slide shift
    double to = offset + elementContainer.ActualWidth;

    if (_currentSlide == _items.Count)
    {
        to = 0;
        _currentSlide = 0;
    }

    DoubleAnimation animation = new DoubleAnimation();
    animation.EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut };

    animation.From = offset;
    animation.To = to;
    animation.Duration = TimeSpan.FromMilliseconds(300);

    myScroll.BeginAnimation(AnimatedScrollControl.SlideOffsetProperty, animation);
}

当到达最后一张幻灯片并将视口(viewport)更新为第一张幻灯片时,它会反向扫描所有幻灯片以到达开头。像这样: Control Examples 02 我想要做的是让它看起来好像视口(viewport)直接从“幻灯片 5”扫描到“幻灯片 1”,例如: Control Examples 03

有没有一种方法可以设置我的 ItemsControlScrollViewer,或者调整我的 Animation 以实现此目的?

最佳答案

我的解决方案是使用 WPF 博士编写的旧 LoopF​​rame 类。

http://drwpf.com/blog/2009/08/05/itemscontrol-n-is-for-natural-user-interface/

关于c# - 通过动画在 WPF ItemsControl 或 ScrollViewer 中循环项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14343338/

相关文章:

c# - F# 与 C# 性能签名以及示例代码

C#:制作安装 WPF 应用程序 (ClickOnce) 和 Windows 服务的安装程序

c# - WPF ItemsControl IsMouseOver 未按预期工作

c# - 禁用面板上的特定控件

ios - Xcode 10 中缺少身份检查器下方的控件检查器

c# - 单元测试 .NET Standard 1.6 库

c# - 使用 ViewModel 中的多个变量绑定(bind) WPF 控制可见性

c# - Order By 与条件的关系的子集

c# - 如何从 DataGridTemplateColumn.CellTemplate 访问按钮

c# - 如何使用 Metro 应用程序 win8 在 xaml 中添加用户控件?