Pavel
Pavel

Reputation: 481

How to create EventSetter launching after animation

I need to change properties of object after the animation ends. I was happy when I read about 'Completed', but when I tried:

<Storyboard>
    <ColorAnimation 
     Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
     To="RoyalBlue"
     Duration="0:0:1"
     Completed="ColorAnimation_Completed"/>
</Storyboard>

I got:

The event 'Completed' cannot be specified on a Target tag in a Style. Use an EventSetter instead.

How to create EventSetter in this case?

Upvotes: 3

Views: 1410

Answers (1)

CThin
CThin

Reputation: 155

if this is being set in a style, you can set your storyboard as a static resource to whatever control you're currently using:

    <UserControl.Resources>
         <storyboard x:Key"Animation">
            <ColorAnimation 
            Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
            To="RoyalBlue"
            Duration="0:0:1"
            Completed="ColorAnimation_Completed"/>
         </Storyboard>
</UserControl.Resources>

Then, on your control:

<Style x:Name="Style" TargetType="{x:Type yourcontroltype}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=whatevertriggeresyouranimation}" Value="triggerValue" >
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource Animation}">
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
       </Style>

You need to fill in a few variables here because I don't see enough of what you are doing, but this will work.

Upvotes: 2

Related Questions