Activate Storyboard on Visibility Changed

2019-04-06 02:47发布

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>

2条回答
做个烂人
2楼-- · 2019-04-06 03:30

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>
查看更多
Animai°情兽
3楼-- · 2019-04-06 03:35

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>
查看更多
登录 后发表回答