views:

384

answers:

1

I try to animate a DependencyProperty from a value to a target value (in code) and when the animation finishes (or is canceled) set the final value to the property. The final value would be either the To value if the animation finishes or the current computed value (by the animation) when the animation is canceled.

By default the Animation doesn't have this behavior and an Animation doesn't change the actual value even if it has completed.

A failed attempt

A while ago I wrote this helper method to achieve the mentioned behavior:

static void AnimateWithAutoRemoveAnimationAndSetFinalValue(IAnimatable element,
    DependencyProperty property,
    AnimationTimeline animation)
{
    var obj = element as DependencyObject;
    if (obj == null)
     throw new ArgumentException("element must be of type DependencyObject");
    EventHandler handler = null;
    handler = (sender, e) =>
    {
     var finalValue = obj.GetValue(property);
     //remove the animation
     element.BeginAnimation(property, null);
     //reset the final value
     obj.SetValue(property, finalValue);

     animation.Completed -= handler;

    };

    animation.Completed += handler;

    element.BeginAnimation(property, animation);
}

Unfortunately the Completed event doesn't seem to fire if the Animation is removed by someone calling BeginAnimation(property,null) and therefore I cannot set the final value correctly when an Animation is canceled. What is worse I cannot remove the event handler either...

Does someone know how to do this in a clean way?

+1  A: 

It looks like you are trying to set value back to the initial value prior to the animation starts just after the animation finishes. This actually happens automatically if you stop the animation (or when the animation timeline finishes). Animations, in WPF, can be though of as an value overlay on top of the current value of the property. Stopping or removing an animation will remove the overlay and the prior value will now start showing through again. The original value is not lost, it is being temporarily obscured.

Setting the property explicitly back to its prior value is, in general, bad practice because it can incur a memory overhead if the value was not originally set locally on the object. Also, this can introduce subtle problems later. For example, if the old value was derived from a style, the property will no longer change if the style changes.

If you still feel you must extract the current value and later restore it, use ReadLocalValue() instead of GetValue(). This will return DepenencyProperty.UnsetValue if the value is not set locally. You can then conditionally call ClearValue() or SetValue() depending on whether the property initially had a local value.

chuckj
Thanks for your answer but this is not what the code does. The 'obj.GetValue(property)' is executed in the Completed event handler and will get the final value of the animation. What I would like to do is set the final value of the animation on the property itself.
Patrick Klug
If you know what the final value will be one way to do this is to set the final value to the local value of the property and then start the animation. When the animation completes the animation layer will be removed and the final value will show through.
chuckj