1

I would like to use a DoubleAnimation to animate a double-DependencyProperty on MouseOver depending on it's current value. For the sake of simplicity lets say i want to animate a ListBoxItem to move slightly to the right while i hover my mouse over it and move back when the mouse is removed.

I started out by defining <DoubleAnimation By="5"> and <DoubleAnimation By="-5"> in the EnterActions and ExitActions of my MouseOver-Trigger. This works fine if i hover the mouse over the object long enough, but if i interrupt the first animation by moving the mouse, the second animation will run longer than the first one. Thus after all animations are done, the objects position is slightly off.

I can use FillBehaviour="Stop" in the Storyboard to fix the state of my object after all animations are done. However this makes the object snap back into place, which obviously looks quite irritating.

What i would like to do is to specify the To-Property in the second DoubleAnimation to be the original value of my Property. This was calculated initially by a Converter, so I tried to bind that Property to the already existing Converter. This throws an error due to threading issues (while i was surprised at first, google explained the reason quite well). I even found a post covering this (WPF animation: binding to the "To" attribute of storyboard animation) and might be able to modify this to fit my situation. However this feels like a "hacky" solution.

The question: I feel like i am not thinking straightforward enough anymore. All I want to do is to "move" an object on mouseover and "move" it back afterwards. Is there an easy way to do this?

Community
  • 1
  • 1
H W
  • 2,556
  • 3
  • 21
  • 45
  • If you **do not** specify `To`, then *actual value* is used (the one which is set, not the one set by animation in hold). Try if it works. If it doesn't, then use combination of stop/hold - enter action animation holds its animation, while exit does stop. – Sinatr Jun 08 '15 at 11:53
  • Neither specifiying `To` nor `By` in the `ExitActions` did the trick. Thank you - this was way easier, than expected! – H W Jun 08 '15 at 11:56

1 Answers1

0

As pointed out by Sinatr in the comments, it is sufficient to just define <DoubleAnimation> (skipping the By="-5") in the ExitActions. This will default to the initial value, not the one that is currently used in the animation.

H W
  • 2,556
  • 3
  • 21
  • 45