我有以下XAML用于使用路由事件的边界触发器
<Border.Triggers>
<EventTrigger RoutedEvent="MouseLeftButtonUp" EnterActions="">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="0" Duration="0:0:0.4" Storyboard.Target="{Binding ElementName=messageWriterDefinition}" Storyboard.TargetProperty="Height">
<DiscreteObjectKeyFrame >
<DiscreteObjectKeyFrame.Value>
<GridLength>20</GridLength>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
...
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
当点击边框和包含元素并且动画导致目标从0到200的高度打开动画时,会触发此触发器
触发器工作得非常好,但每次边框接收到动画运行的事件,目标动画再次打开(即使已经打开)
如何为有效忽略动画的触发器添加条件,目标的高度是否大于零?
答案 0 :(得分:1)
您可以使用DoubleAnimation而不是ObjectAnimationUsingKeyFrames。通过仅设置其To
属性,而不是From
,动画从当前属性值开始。它还要求您还设置边框的Height
的初始值:
<Border Height="20" ...>
<Border.Triggers>
<EventTrigger RoutedEvent="MouseLeftButtonUp">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Height"
To="200" Duration="0:0:0.4"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
</Border>