wpf 在可见性改变时激活故事板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22397819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Activate Storyboard on Visibility Changed
提问by David Pilkington
Currently I have an Imagethat 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.Triggershave to be EventTriggers.
目前我有一个Image在加载时我会脉冲。我想在更改图像的可见性时激活故事板。但我认为Image.Triggers必须如此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>
采纳答案by Anatoliy Nikolaev
In WPF there is an event UIElement.IsVisibleChangedbut is a CLR event, not a routed event, therefore in EventTriggercan not be used.
在 WPF 中有一个事件UIElement.IsVisibleChanged但是是一个 CLR 事件,而不是一个路由事件,因此 inEventTrigger不能使用。
In this case use IsVisibleproperty in DataTriggerlike this:
在这种情况下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>
回答by Richard E
If you want to animate on a DependencyPropertyor bound value you will need to create a Stylefor your image and put the animation in the style.
如果你想在一个DependencyProperty或绑定的值上设置动画,你需要Style为你的图像创建一个并将动画放在样式中。
The following Stylewill perform your animation when the Visibilityof your image is set to Visible:
Style当Visibility您的图像设置为时,以下将执行您的动画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>

