WPF - RectangleGeometry animation in C# using a Storyboard

71 Views Asked by At

I need to animate a RectangleGeometry in C#, but I don't know what is the path to use for the ProperyPath.

rectAnimation.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("What-is-the-path?"));

XAML:

<Image x:Name="Item" Source="{StaticResource ItemDragComplete00bw}" Stretch="None">
   <Image.Clip>
      <RectangleGeometry Rect="0, 0, 684, 250" RadiusX="10" RadiusY="10">
         <RectangleGeometry.Transform>
            <TransformGroup>
               <ScaleTransform CenterX="342" CenterY="0" ScaleX="0.366" ScaleY="1.0"/>
            </TransformGroup>
         </RectangleGeometry.Transform>
      </RectangleGeometry>
   </Image.Clip>
</Image>

C#

RectAnimation rectAnimation = new RectAnimation();
rectAnimation.Duration = TimeSpan.FromMilliseconds(toMills - fromMills);
rectAnimation.FillBehavior = FillBehavior.HoldEnd;
rectAnimation.From = new Rect(xFrom, yFrom, wFrom, hFrom);
rectAnimation.To = new Rect(xTo, yTo, wTo, hTo);

rectAnimation.SetValue(Storyboard.TargetProperty, target);
rectAnimation.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("What-is-the-path?"));

storyboard.Children.Add(rectAnimation);

Thanks.

1

There are 1 best solutions below

6
Clemens On

You do not need a Storyboard. Just call

var geometry = (RectangleGeometry)Item.Clip;
geometry.BeginAnimation(RectangleGeometry.RectProperty, rectAnimation);

Also note that it is usually not necessary to set the animation's From property when you simply want to animate from the current value to a new value. The default value of the FillBehavior property is also already HoldEnd.