Search code examples
wpfsilverlightanimationprismseparation-of-concerns

Dynamic animations, Commands and separation of concerns


Scenario: I have a (numeric) textbox, a button, and a label. When the button is clicked I'd like the label to "animate" to the numeric value in the textbox (like a spinning dial)

Given:

a) that animations in storyboards cannot have databindings (because they are not FrameworkElements) b) the lack of triggers in Silverlight

What is the best, and with least coupling of the view model to the view's storyboard, way to update the target animation value and start the animation when the button is clicked?

Note: The scenario is conceptual, so don't concentrate on the specifics of 'animating' numbers or anything


Solution

  • If your goal is strictly to reduce the code-behind in the view I think that an attached behaviour on the Label would work for this. The attached behaviour on the label would expose the number to be animated to and when this number changes an animation (in code) would be run to animate from the old value to the new value.

    One drawback is that your animation is now in code, unless you store a templated (just has fake values to start with) version of it in a resource file somewhere where you can load it as needed and replace the templated values.

    This article from Josh Smith seems to be the authority on Attached Behaviours;

    http://joshsmithonwpf.wordpress.com/2008/08/30/introduction-to-attached-behaviors/