Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activate Storyboard on Visibility Changed

Currently I have an Image that I pulse when it is loaded. I want to rather activate the Storyboard when I change the Visibility of the Image. But I see that Image.Triggers have to be EventTriggers.

Which event do I need to hook into?

<Image.Triggers>
    <EventTrigger RoutedEvent="Image.Loaded">
        <BeginStoryboard>
             <Storyboard>
                 <DoubleAnimation Storyboard.TargetName="MandateErrorImage"
                                  Storyboard.TargetProperty="Opacity"
                                  From="1.0" To="0.1" Duration="0:0:0.5"
                                  AutoReverse="True" RepeatBehavior="0:0:2" />
             </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Image.Triggers>
like image 816
David Pilkington Avatar asked Mar 14 '14 06:03

David Pilkington


2 Answers

In WPF there is an event UIElement.IsVisibleChanged but is a CLR event, not a routed event, therefore in EventTrigger can not be used.

In this case use IsVisible property in DataTrigger like this:

<Window.Resources>
    <Style x:Key="AnimationImageStyle" TargetType="{x:Type Image}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=IsVisible}" 
                         Value="True">

                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                             From="1.0" To="0.1"
                                             Duration="0:0:0.5"
                                             AutoReverse="True"
                                             RepeatBehavior="0:0:2" />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <Image Name="TestImage" 
           Style="{StaticResource AnimationImageStyle}" 
           Source="Enter.jpg"
           Width="400"
           Height="400" />        
</Grid>
like image 71
Anatoliy Nikolaev Avatar answered Oct 19 '22 00:10

Anatoliy Nikolaev


If you want to animate on a DependencyProperty or bound value you will need to create a Style for your image and put the animation in the style.

The following Style will perform your animation when the Visibility of your image is set to Visible:

    <Style x:Key="FlashingImageStyle" TargetType="{x:Type Image}">
        <Style.Triggers>
            <Trigger Property="Visibility" Value="Visible">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation
                                  Storyboard.TargetProperty="Opacity"
                                  From="1.0" To="0.1" Duration="0:0:0.5"
                                  AutoReverse="True" RepeatBehavior="0:0:2" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
        </Style.Triggers>
    </Style>
like image 38
Richard E Avatar answered Oct 18 '22 23:10

Richard E