This documentation is archived and is not being maintained.

How to: Animate a Matrix by Using Key Frames

This example shows how to animate the Matrix property of a MatrixTransform by using key frames.

The following example uses the MatrixAnimationUsingKeyFrames class to animate the Matrix property of a MatrixTransform. The example uses the MatrixTransform object to transform the appearance and position of a Button.

This animation uses the DiscreteMatrixKeyFrame class to create two key frames and does the following with them:

  1. Animates the first Matrix during the first 0.2 seconds. The example changes the M11 and M12 properties of the Matrix. This change causes the button to stretch and become skewed. The example also changes the OffsetX and OffsetY properties so that the button changes position.

  2. Animates the second Matrix at 1.0 seconds. The button moves to another position while the button is no longer skewed or stretched.

  3. Repeats the animation indefinitely.


Key frames that derive from the DiscreteMatrixKeyFrame object create sudden jumps between values, that is, the movement of the animation is jerky.

  Title="MatrixAnimationUsingPath Example">
  <StackPanel Margin="20">
    <Canvas HorizontalAlignment="Left" Width="340" Height="240" >

      <!-- The Button that is animated. -->
      <Button Margin="-30,0,0,0" MinWidth="100">
          <MatrixTransform x:Name="myMatrixTransform">
            <MatrixTransform.Matrix >
              <Matrix OffsetX="10" OffsetY="100"/>
          <EventTrigger RoutedEvent="Button.Loaded">

                <!-- Animates the button's MatrixTransform using MatrixAnimationUsingKeyFrames. 
                Animates to first Matrix in the first 0.2 seconds, to second Matrix in the next
                second, and then starts over. Notice that the first KeyFrame stretches the button
                and skews it using the M11 and M12 Matrix properties respectively. Also, animations are 
                using Discrete interpolation, so the MatrixTransform appears to "jump" from one value 
                to the next. -->
                  <DiscreteMatrixKeyFrame KeyTime="0:0:0.2">
                      <Matrix OffsetX="100" OffsetY="200" M11="3" M12="1" />
                  <DiscreteMatrixKeyFrame KeyTime="0:0:1">
                      <Matrix OffsetX="300" OffsetY="100" M11="1" M12="0" />



For the complete sample, see KeyFrame Animation Sample.