//
// This control is created by Ashley Davis and copyrighted under CPOL, and available as part of
// a CodeProject article at
// http://www.codeproject.com/KB/WPF/zoomandpancontrol.aspx
// This code is based on the article dated: 29 Jun 2010
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.Animation;
namespace SharpVectors.Runtime
{
///
/// A helper class to simplify animation.
///
public static class ZoomPanAnimationHelper
{
///
/// Starts an animation to a particular value on the specified dependency property.
///
public static void StartAnimation(UIElement animatableElement, DependencyProperty dependencyProperty,
double toValue, double animationDurationSeconds)
{
StartAnimation(animatableElement, dependencyProperty, toValue, animationDurationSeconds, null);
}
///
/// Starts an animation to a particular value on the specified dependency property.
/// You can pass in an event handler to call when the animation has completed.
///
public static void StartAnimation(UIElement animatableElement, DependencyProperty dependencyProperty,
double toValue, double animationDurationSeconds, EventHandler completedEvent)
{
double fromValue = (double)animatableElement.GetValue(dependencyProperty);
DoubleAnimation animation = new DoubleAnimation();
animation.From = fromValue;
animation.To = toValue;
animation.Duration = TimeSpan.FromSeconds(animationDurationSeconds);
animation.Completed += delegate(object sender, EventArgs e)
{
//
// When the animation has completed bake final value of the animation
// into the property.
//
animatableElement.SetValue(dependencyProperty, animatableElement.GetValue(dependencyProperty));
CancelAnimation(animatableElement, dependencyProperty);
if (completedEvent != null)
{
completedEvent(sender, e);
}
};
animation.Freeze();
animatableElement.BeginAnimation(dependencyProperty, animation);
}
///
/// Cancel any animations that are running on the specified dependency property.
///
public static void CancelAnimation(UIElement animatableElement, DependencyProperty dependencyProperty)
{
animatableElement.BeginAnimation(dependencyProperty, null);
}
}
}