Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 151387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:26:30+00:00 2026-05-11T09:26:30+00:00

I try to animate a DependencyProperty from a value to a target value (in

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:26:30+00:00Added an answer on May 11, 2026 at 9:26 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We try to convert from string to Byte[] using the following Java code: String
when i try to animate custom View (created in java code), there is no
I try to write KSH script for processing a file consisting of name-value pairs,
I try Request.Form.Set(k, v) but it's throwing exception Collection is read-only
For some reason, when I try to animate textColor , it won't work. The
i try to animate a TextView on a changeText But always see only one
Consider the following simple WPF form, we will try to animate border1's Height: This
I have UIView i set it's Alpha to Zero to animate it to be
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
try { ... } catch (SQLException sqle) { String theError = (sqle).getSQLState(); ... }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.