using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace Muchinfo.WPF.Controls.Animation
{
///
/// Class RollOverAnimation.
///
[TemplatePart(Name = "NextItems", Type = typeof(ItemsControl))]
[TemplatePart(Name = "CurrentItems", Type = typeof(ItemsControl))]
public class RollOverAnimation : Control
{
static RollOverAnimation()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(RollOverAnimation), new FrameworkPropertyMetadata(typeof(RollOverAnimation)));
}
private const int C_steySeconds = 3;
private const int C_PlaySeconds = 5;
private Storyboard _rolloverStoryboard = new Storyboard();
private DoubleAnimationUsingKeyFrames _currentAnimation;
private DoubleAnimationUsingKeyFrames _nextAnimation;
private ItemsControl _currentItems, _nextItems;
///
/// 当前索引
///
public int _currentIndex;
public RollOverAnimation()
{
_currentAnimation = new DoubleAnimationUsingKeyFrames();
_nextAnimation = new DoubleAnimationUsingKeyFrames();
_rolloverStoryboard.Completed += _rolloverStoryboard_Completed;
IsVisibleChanged += delegate(object sender, DependencyPropertyChangedEventArgs e)
{
if (false.Equals(e.NewValue))
{
_rolloverStoryboard.Stop(this);
}
};
}
///
/// 动画完成后切换
///
///
///
void _rolloverStoryboard_Completed(object sender, EventArgs e)
{
_rolloverStoryboard.Stop(this);
PlayNextList();
_rolloverStoryboard.Begin(this, true);
}
///
/// 显示行的样式
///
public DataTemplate ItemsTemplate
{
get { return (DataTemplate)GetValue(ItemsTemplateProperty); }
set { SetValue(ItemsTemplateProperty, value); }
}
// Using a DependencyProperty as the backing store for ItemsTemplate. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ItemsTemplateProperty =
DependencyProperty.Register("ItemsTemplate", typeof(DataTemplate), typeof(RollOverAnimation), new PropertyMetadata(null));
///
/// 绑定的数据源
///
public IList ItemsSource
{
get { return (IList)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
// Using a DependencyProperty as the backing store for ItemsSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IList), typeof(RollOverAnimation), new PropertyMetadata(null, OnItemsSourceChange));
///
/// 当前显示的项
///
public IList CurrentList
{
get { return (IList)GetValue(CurrentListProperty); }
set { SetValue(CurrentListProperty, value); }
}
// Using a DependencyProperty as the backing store for CurrentList. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CurrentListProperty =
DependencyProperty.Register("CurrentList", typeof(IList), typeof(RollOverAnimation), new PropertyMetadata(null));
///
/// 即将显示的项
///
public IList NextList
{
get { return (IList)GetValue(NextListProperty); }
set { SetValue(NextListProperty, value); }
}
// Using a DependencyProperty as the backing store for NextList. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NextListProperty =
DependencyProperty.Register("NextList", typeof(IList), typeof(RollOverAnimation), new PropertyMetadata(null));
///
/// 每次显示的数量
///
public int ShowCount
{
get { return (int)GetValue(ShowCountProperty); }
set { SetValue(ShowCountProperty, value); }
}
// Using a DependencyProperty as the backing store for ShowIndex. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ShowCountProperty =
DependencyProperty.Register("ShowCount", typeof(int), typeof(RollOverAnimation), new PropertyMetadata(3));
///
/// 当数据源改变时重新进行动画
///
///
///
public static void OnItemsSourceChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var self = d as RollOverAnimation;
if (self != null)
{
self.InitSourceAndPlay();
}
}
///
/// 初始化数据并运行动画
///
private void InitSourceAndPlay()
{
if (ItemsSource != null && ItemsSource.Count > 0)
{
_rolloverStoryboard.Stop(this);
_currentIndex = 0;
if (ItemsSource.Count > ShowCount)
{
var list = new List