Search code examples
c#wpfanimationbrushlineargradientbrush

WPF C# Animate Linear Gradient Brush start point or end point


How can you set the animation of "EndPointProperty" or "StartPointProperty" to animate LinearGradientBrush?

I have this code in xaml:

<Rectangle x:Name="itemRefl"  >
 <Rectangle.Fill>
  <LinearGradientBrush EndPoint="1,0" StartPoint="0,0">
    <GradientStop Offset="0"/>
    <GradientStop Color="White" Offset="0.5"/>
    <GradientStop Offset="1"/>
  </LinearGradientBrush>
 </Rectangle.Fill>
</Rectangle>

And my code for animation is this but not work without errors. How I can animate this property correctly?

Storyboard story1 = new Storyboard();
PointAnimation endPointAnim = new PointAnimation()
{
   EasingFunction = new SineEase { EasingMode = EasingMode.EaseInOut },
   From = new Point( 0.0, -0.26),
   To = new Point(0.0, 0.26),
   Duration = new Duration(TimeSpan.FromMilliseconds(500))

};
Storyboard.SetTargetProperty(endPointAnim, new PropertyPath(LinearGradientBrush.EndPointProperty));
story1.Children.Add(endPointAnim);
myelement.BeginStoryboard(story1);

Thank you


Solution

  • Run the animation directly on the LinearGradientBrush:

    itemRefl.Fill.BeginAnimation(LinearGradientBrush.EndPointProperty, endPointAnim);