Dynamic Animations, Commands and Separation of Concerns

Scenario: I have a (numeric) textbox, button and shortcut. When the button is clicked, I would like the label to "animate" the numeric value in the text box (like a spinning disc)

Given:

a) that animations in storyboards cannot have data bindings (since they are not FrameworkElements) b) no triggers in Silverlight

What's the best and minimal combination of view model with storyboard view, a way to update the target value of the animation and trigger the animation when the button is clicked?

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

+2


source to share


2 answers


If your goal is to minify the code in the view, I think the bound behavior on the label will work for that. The attached behavior on the label will display a number to be animated, and when that number changes the animation (in code), it will run to animate from the old value to the new value.

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



This article by Josh Smith appears to be the authority at Attached Behaviors;

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

+2


source


I recently had to solve a similar problem in an MVVM application. My problem was that I needed to animate the height of the container from zero to auto. Since Auto is a dynamic value, I realized that the animation (or storyboard) would need to be created (or manipulated) on demand. The solution I have used involves using the view code to update and run the animation.

This is not the most MVVM approach; however, animations in WPF can be complex in XAML. Since this solution is really just a workaround for the XAML restriction, it seems to correctly bind the code directly to the view. Likewise, if the views were mocked, then there would be no frame elements to animate, so it really wouldn't make sense to put that code on the VM side.



Does anyone have a better approach?

0


source







All Articles