c# - 在可见性更改时激活 Storyboard

标签 c# wpf xaml mvvm storyboard

目前我有一个 Image,当它被加载时我会发出脉冲。我想在更改图像的可见性时激活 Storyboard。但我看到 Image.Triggers 必须是 EventTriggers

我需要挂接到哪个事件?

<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>

最佳答案

在 WPF 中有一个事件 UIElement.IsVisibleChanged但它是 CLR 事件,而不是路由事件,因此在 EventTrigger 中不能使用。

在这种情况下使用 IsVisible DataTrigger 中的属性如下所示:

<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>

关于c# - 在可见性更改时激活 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22397819/

相关文章:

c# - 如何没有/一个窗口标题并为任务栏设置不同的标题?

c# excel在excel工作表上创建一个按钮

wpf - 依赖属性回调不起作用

xaml - Visual Studio 2012 XAML 设计器似乎没有更新

c# - 不同日期列表

c# - 将值与 null 进行比较时出现异常

wpf - 如何在 DataGridTextColumn 中实现文本换行

c# - WPF MVVM MahApps范围 slider ,工具提示中带有日期

c# - 将 UID 属性添加到我的 XAML 文件

c# - 自动生成 xaml ResourceDictionary