当在 ItemsControl
或 ScrollViewer
中移动项目时,我能否循环内容以便第一个项目出现在最后一个项目“之后”(如用户所感知的那样) )?
我有一个自定义的 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)。计时器会自动移动到下一个项目。
移动幻灯片的代码(为简洁起见进行了简化)目前如下所示:
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)更新为第一张幻灯片时,它会反向扫描所有幻灯片以到达开头。像这样: 我想要做的是让它看起来好像视口(viewport)直接从“幻灯片 5”扫描到“幻灯片 1”,例如:
有没有一种方法可以设置我的 ItemsControl
或 ScrollViewer
,或者调整我的 Animation
以实现此目的?
最佳答案
我的解决方案是使用 WPF 博士编写的旧 LoopFrame
类。
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/