Activate Storyboard on Visibility Changed

前端 未结 2 1188
渐次进展
渐次进展 2021-02-20 15:02

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 I

相关标签:
2条回答
  • 2021-02-20 15:06

    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>
    
    0 讨论(0)
  • 2021-02-20 15:18

    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>
    
    0 讨论(0)
提交回复
热议问题