當在ItemsControl
或ScrollViewer
中移動項目時,我是否可以循環內容,以便第一個項目在「最後一個項目之後」(由用戶感知)之後出現?通過動畫在WPF 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
每個項目佔據了整個可見區域的控制,使用戶一次只能看到一個項目。下面的圖片表示正在操作的控件,紅色框表示視口。計時器自動移動到下一個項目。 移位的幻燈片中的代碼(簡化爲簡潔起見)目前看起來像這樣:
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);
}
當到達最後幻燈片和視口被更新到所述第一幻燈片,它會掃描所有的幻燈片在反向到達開始。像這樣: 我想要做的就是讓它看起來好像視口直接從「幻燈片5」掃描到「幻燈片1」,如:
有沒有一種方法我可以設置我的ItemsControl
或ScrollViewer
,或調整我的Animation
以實現此目的?