目前我有一个 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/