AXG1010
AXG1010

Reputation: 1962

WPF Storyboard Animation Loops Forever Even After Being Set

I have a a custom user control to animate based on a DependencyProperty which is bound to a DataTrigger. If the DependencyProperty is equal to Failure, it should animate the fill color of a rectangle (named buttonColor) within the user control.

For some reason though, it always loops forever even if I set the RepeatBehavior to any value including 1.

If I remove the RepeatBehavior attribute, it only plays the animation once (as expected). Here is the code which I have the issue:

<DataTrigger Binding="{Binding Path=ButtonAction.Status}" Value="Failure">
    <DataTrigger.EnterActions>
        <StopStoryboard BeginStoryboardName="Pulse"/>

        <BeginStoryboard>
            <Storyboard RepeatBehavior="1">
                <ColorAnimation Storyboard.TargetName="buttonColor"
                                Storyboard.TargetProperty="Fill.Color" 
                                To="{StaticResource FailedColor}" 
                                AutoReverse="True" />
            </Storyboard>
        </BeginStoryboard>
    </DataTrigger.EnterActions>
</DataTrigger>

Upvotes: 15

Views: 19950

Answers (3)

ΩmegaMan
ΩmegaMan

Reputation: 31721

To be clear and to add onto the answer, if one actually wants to repeat continuously one can set the RepeatBehavior to Forever such as here where I am rotating a vector around its center point.

<Storyboard x:Key="ChaseRotate" RepeatBehavior="Forever">
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="path">
         <EasingDoubleKeyFrame KeyTime="0:0:1" Value="360"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

See How to: Rotate an Object - WPF .NET Framework

Full Example for Chasing Circles in Xaml.

Upvotes: 0

john-g
john-g

Reputation: 954

Setting a duration value will also limit the repeat behavior as it takes precedence. So if you have repeat behavior set on the ColorAnimationUsingKeyFrames tag but on the storyboard you set a Duration="0:0:4" then the animation will only repeat for 4 seconds.

Upvotes: 1

Fede
Fede

Reputation: 44048

The correct syntax to repeat N times is:

<Storyboard RepeatBehavior="Nx">

for example:

<Storyboard RepeatBehavior="6x">

Upvotes: 46

Related Questions