views:

56

answers:

2

I'll try to make this as simple as I can.

I want to do this:

<Storyboard x:Name="MoveToLocation">
   <DoubleAnimation Duration="0:0:0.5" To="{Binding X}" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="grid" d:IsOptimized="True"/>
</Storyboard>

As you may have noticed the Binding on 'To' Property does not work. It seems to only accept static values.

How does one do this animation with MVVM? I cant just put in static data, cause it's going to change.

Thanks.

A: 

If you're trying to do it in a Style, that's why it's not working. Silverlight will Freeze it because it's shared across all instances of that style.

Try putting it in the element itself and that should work.

Christopher Estep
sorry for not showing enough code, but it's not in a style. It is, however, in a DataTemplate. (I'm going to have lots of instances of objects that need to be animated)
Peanut
+1  A: 

DoubleAnimation is also not a FrameworkElement hence binding doesn't work to that either. See my answer to your previous question.

In order to achieve your goal here you will need to take the contents of your DataTemplate and turn it into a UserControl. This new control can then expose a set of dependency properties that you need to bind to, including one for To value of the storyboard.

AnthonyWJones
That second paragraph is what I needed you to elaborate on, thanks. I will try this when I get home this evening. So are you suggesting I should contain a Storyboard instance inside the object itself (not in the xaml)? Seems kinda strange, but If that's what I have to do, then I'll do it.
Peanut
I guess I'm still a little confused - I could wrap this whole 'object' into a usercontrol and create my own Property called 'To', thats fine. However, somewhere, i'll still need to set (and set again) the property of 'To' in my storyboard - which, like you said, is not bindable.
Peanut
@Peanut: Once you have created a UserControl to encapsultate this specific peice of UI behaviour you can manipulate the control with "code-behind" directly. Sometimes binding just isn't the answer and you actually have to write code that does the job.
AnthonyWJones
@AnthonyWJones: Interesting. It seems as if the MVVM approach is still a little immature then. I think I have the information to mark this as the answer. Thanks for your time.
Peanut