我有一组在列表框中正确显示的元素。我想对列表进行动画处理,以便它像电影结尾的滚动字幕一样显示。此外,我想让列表循环,这样当最后一个项目在列表框的可见部分滚动时,它会显示下一个项目。坦率地说,我不知道从哪里开始。
感谢您的帮助和关注。
您可以为此使用动画。
XAML:
<Canvas Name="_canvas">
<ListBox Name="_listBox" Loaded="OnListBoxLoaded" BorderBrush="Transparent">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Blue" BorderThickness="2" CornerRadius="10" Margin="5, 20, 5, 0">
<TextBlock Text="{Binding}" Margin="10" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Canvas>
后面的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
_listBox.ItemsSource = new List<string>() { "FFF", "EEE", "DDD", "CCC", "BBB", "AAA" }; ;
}
private void OnListBoxLoaded(object sender, RoutedEventArgs e)
{
DoubleAnimation doubleAnimation = new DoubleAnimation();
doubleAnimation.From = -(sender as FrameworkElement).ActualHeight;
doubleAnimation.To = _marqueeHeight;
doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
doubleAnimation.Duration = new Duration(TimeSpan.Parse("0:0:10"));
_listBox.BeginAnimation(Canvas.TopProperty, doubleAnimation);
_canvas.Clip = new RectangleGeometry(new Rect(0, 0, _canvas.ActualWidth, _marqueeHeight));
}
private double _marqueeHeight = 100;
}