I am looking for a clean way to start an animation that will have dynamic values. Basically I want to do an animation where an element changes width based on the data of another element. Say I have a TextBlock that's Text Property is Binding. When this property changes I want a visual element say a Rectangle for our sake to do a DoubleAnimation changing the width from previous value to the new value.
I am trying to stay away from putting code in my view if possible. I've looked into DataTriggers but they seem to require that you know what the value would be such as an Enum. In my case it is just the value changing that needs to trigger a storyboard and the animation would need to start at the current(previous) value and move nicely to the new value.
Any ideas. Maybe I just missed a property.
Actually you want to bind the
DoubleAnimation.ToProperty
to theViewModel
property and animate actual control. The problem is animation should be continued whenToProperty
changed. My solution encapsulate all this logic to aMarkupExtenstion
which wraps aBinding
.You can do the same with other animation classes to animate other types.
Here is the solution I ended up with. To do the Animation based on data in my ViewModel I used a DataTrigger. Below is my Style for the control.
Since properties modified by animation cannot be set outside the animation 'context', I came up with a code solution since I could not do the same in XAML effectively.
You could explore using Attached Properties to hook up the necessary logic to the Storyboard/Animation that you desire.
This won't necessarily stop you from having to write code, but it will keep it separated from the view and allow it to be re-used across multiple views.