[12762] | 1 | // <copyright>
|
---|
| 2 | // This control is created by Ashley Davis and copyrighted under CPOL, and available as part of
|
---|
| 3 | // a CodeProject article at
|
---|
| 4 | // http://www.codeproject.com/KB/WPF/zoomandpancontrol.aspx
|
---|
| 5 | // <date>This code is based on the article dated: 29 Jun 2010</date>
|
---|
| 6 | // </copyright>
|
---|
| 7 |
|
---|
| 8 | using System;
|
---|
| 9 | using System.Collections.Generic;
|
---|
| 10 | using System.Linq;
|
---|
| 11 | using System.Text;
|
---|
| 12 | using System.Windows;
|
---|
| 13 | using System.Windows.Media.Animation;
|
---|
| 14 |
|
---|
| 15 | namespace SharpVectors.Runtime
|
---|
| 16 | {
|
---|
| 17 | /// <summary>
|
---|
| 18 | /// A helper class to simplify animation.
|
---|
| 19 | /// </summary>
|
---|
| 20 | public static class ZoomPanAnimationHelper
|
---|
| 21 | {
|
---|
| 22 | /// <summary>
|
---|
| 23 | /// Starts an animation to a particular value on the specified dependency property.
|
---|
| 24 | /// </summary>
|
---|
| 25 | public static void StartAnimation(UIElement animatableElement, DependencyProperty dependencyProperty,
|
---|
| 26 | double toValue, double animationDurationSeconds)
|
---|
| 27 | {
|
---|
| 28 | StartAnimation(animatableElement, dependencyProperty, toValue, animationDurationSeconds, null);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// Starts an animation to a particular value on the specified dependency property.
|
---|
| 33 | /// You can pass in an event handler to call when the animation has completed.
|
---|
| 34 | /// </summary>
|
---|
| 35 | public static void StartAnimation(UIElement animatableElement, DependencyProperty dependencyProperty,
|
---|
| 36 | double toValue, double animationDurationSeconds, EventHandler completedEvent)
|
---|
| 37 | {
|
---|
| 38 | double fromValue = (double)animatableElement.GetValue(dependencyProperty);
|
---|
| 39 |
|
---|
| 40 | DoubleAnimation animation = new DoubleAnimation();
|
---|
| 41 | animation.From = fromValue;
|
---|
| 42 | animation.To = toValue;
|
---|
| 43 | animation.Duration = TimeSpan.FromSeconds(animationDurationSeconds);
|
---|
| 44 |
|
---|
| 45 | animation.Completed += delegate(object sender, EventArgs e)
|
---|
| 46 | {
|
---|
| 47 | //
|
---|
| 48 | // When the animation has completed bake final value of the animation
|
---|
| 49 | // into the property.
|
---|
| 50 | //
|
---|
| 51 | animatableElement.SetValue(dependencyProperty, animatableElement.GetValue(dependencyProperty));
|
---|
| 52 | CancelAnimation(animatableElement, dependencyProperty);
|
---|
| 53 |
|
---|
| 54 | if (completedEvent != null)
|
---|
| 55 | {
|
---|
| 56 | completedEvent(sender, e);
|
---|
| 57 | }
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | animation.Freeze();
|
---|
| 61 |
|
---|
| 62 | animatableElement.BeginAnimation(dependencyProperty, animation);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /// <summary>
|
---|
| 66 | /// Cancel any animations that are running on the specified dependency property.
|
---|
| 67 | /// </summary>
|
---|
| 68 | public static void CancelAnimation(UIElement animatableElement, DependencyProperty dependencyProperty)
|
---|
| 69 | {
|
---|
| 70 | animatableElement.BeginAnimation(dependencyProperty, null);
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | }
|
---|