I'm messing around with some Storyboards in a Metro XAML app. I have to create a Storyboard
in code. I'd like to set the Storyboard.TargetProperty
to CompositeTransform.Rotation
It seems impossible...
My Storyboard in XAML looks like this:
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="60"/
</DoubleAnimationUsingKeyFrames>
</Storyboard>
I'd like to create something similar.
Important: I am not trying to recreate this exact Storyboard. I am inside the code of a custom ContentControl
, so this
is the Control
, and there's no "grid" to target the animation to. The target is the control itself, which has CompositeTransform
previously set.
My code so far is like this:
var turnSB = new Storyboard();
var doubleAnim = new DoubleAnimationUsingKeyFrames();
doubleAnim.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = TimeSpan.FromMilliseconds(0), Value = currentAngle });
doubleAnim.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = TimeSpan.FromMilliseconds(500), Value = targetAngle });
turnSB.Children.Add(doubleAnim);
Storyboard.SetTarget(doubleAnim, this.RenderTransform);
Storyboard.SetTargetProperty(doubleAnim, "(CompositeTransform.Rotation)");
turnSB.Begin();
As soon as it hits the Begin method I get an Exception saying that (CompositeTransform.Rotation) cannot be resolved. So I'm guessing I didn't got the property path quite right. I've tried different variations, but according to PropertyPaths, this should be the correct one, shouldn't it? :S
If this is an unsolvable problem, I'm open to suggestions on a workaround...
EDIT:
I think I have solved the problem for now. I have some interesting findings though...
If I make a UserControl I can do practically anything. Everything works, I can set the Storyboard.Targetproperty, and the animation plays correctly.
However if I use a custom control, or inherit from another control (say ContentControl), I can't start a Storyboard from code, only in some cases.
For example: If I make a storyboard (defined in XAML) to animate Rotation (or any transformation property for that matter) and try to start from code, I get the above exception. But If I animate a simple property, say Opacity, it works fine.
(I did the same with a UserControl, and it worked.)
Can someone explain this?