views:

949

answers:

2

I have a Canvas which I would need to animate the RenderTransform property of. The start and end matrices will be abitrary, so I can't pre write the storyboard in XAML, so I'm trying to do it in code, I can't find any example of how to do this, below is my best try which does not work (it compiles and runs, but the rendertransform does not change).

Any suggestions on how this should be done?

MatrixAnimationUsingKeyFrames anim = new MatrixAnimationUsingKeyFrames();
MatrixKeyFrameCollection keyframes = new MatrixKeyFrameCollection();
DiscreteMatrixKeyFrame start = new DiscreteMatrixKeyFrame(fromMatrix, KeyTime.FromPercent(0));
DiscreteMatrixKeyFrame end = new DiscreteMatrixKeyFrame(toMatrix, KeyTime.FromPercent(1));

keyframes.Add(start);
keyframes.Add(end);
anim.KeyFrames = keyframes;

Storyboard.SetTarget(anim, World.RenderTransform);
Storyboard.SetTargetProperty(anim, new PropertyPath("Matrix"));

Storyboard sb = new Storyboard();
sb.Children.Add(anim);
sb.Duration = TimeSpan.FromSeconds(4);
sb.Begin();
+1  A: 

Hi, I managed to get matrixtransform working by setting rendersource and using beginanimation

something like this:

        this.matrixTransform = new MatrixTransform();
        this.canvas.RenderTransform = this.matrixTransform;


        MatrixAnimationUsingKeyFrames anim = new MatrixAnimationUsingKeyFrames();
        anim.KeyFrames = new MatrixKeyFrameCollection();
        anim.Duration = TimeSpan.FromSeconds(4);

        Matrix fromMatrix = new Matrix(2, 0, 0, 2, 0, 0);
        Matrix toMatrix =  new Matrix(3, 0, 0, 3, 0, 0);

        anim.FillBehavior = FillBehavior.HoldEnd;
        DiscreteMatrixKeyFrame start = new DiscreteMatrixKeyFrame(fromMatrix, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)));
        DiscreteMatrixKeyFrame end = new DiscreteMatrixKeyFrame(toMatrix, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(4)));

        anim.KeyFrames.Add(start);
        anim.KeyFrames.Add(end);

        this.matrixTransform.BeginAnimation(MatrixTransform.MatrixProperty, anim);

Not sure exactly how I'm going to do the interpolation for all the keyframes myself though :)

MattiasK
+1  A: 

I bumped into this problem this morning, although the solution I used won't cope with rotations or shearing. link

LukeN
That's great for my issue at least which was, scaling and translation.
Twelve47