/// <summary> /// Animates the target color brush to a given color /// </summary> /// <param name="solidColorBrush">The brush to animate</param> /// <param name="toColor">The target color to set</param> /// <param name="ms">The duration of the animation</param> /// <param name="easing">The easing function to use</param> public static void AnimateColor(this SolidColorBrush solidColorBrush, String toColor, int ms, EasingFunctionNames easing) { // Get the target color Color targetColor = ColorConverter.String2Color(toColor); if (solidColorBrush.Color.Equals(targetColor)) { return; } // Prepare the animation ColorAnimation animation = new ColorAnimation { From = solidColorBrush.Color, To = targetColor, Duration = new Duration(TimeSpan.FromMilliseconds(ms)), EasingFunction = easing.ToEasingFunction() }; Storyboard.SetTarget(animation, solidColorBrush); Storyboard.SetTargetProperty(animation, "Color"); Storyboard storyboard = new Storyboard(); storyboard.Children.Add(animation); storyboard.Begin(); }
/// <summary> /// Prepares a an animation with the given info /// </summary> /// <param name="target">The target object to animate</param> /// <param name="property">The property to animate inside the target object</param> /// <param name="from">The initial property value</param> /// <param name="to">The final property value</param> /// <param name="ms">The duration of the animation</param> /// <param name="easing">The easing function to use inside the animation</param> /// <param name="enableDependecyAnimations">Indicates whether or not to apply this animation to elements that need the visual tree to be rearranged</param> public static DoubleAnimation CreateDoubleAnimation(DependencyObject target, String property, double?from, double?to, int ms, EasingFunctionNames easing = EasingFunctionNames.Linear, bool enableDependecyAnimations = false) { DoubleAnimation animation = new DoubleAnimation { From = from, To = to, Duration = new Duration(TimeSpan.FromMilliseconds(ms)), EnableDependentAnimation = enableDependecyAnimations }; if (easing != EasingFunctionNames.Linear) { animation.EasingFunction = easing.ToEasingFunction(); } Storyboard.SetTarget(animation, target); Storyboard.SetTargetProperty(animation, property); return(animation); }