WPF routedevent 情节提要仅在高度为零时才开始

WPF routedevent storyboard begin only if height is zero

我有以下 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>

单击边框和包含元素时会触发此触发器,并且动画会导致目标以动画方式从零高度打开到 200

触发器工作得很好,但每次边框收到事件时,动画都会运行并且目标动画会再次打开(即使已经打开)

如果目标的高度已经大于零,如何向触发器添加一个条件以有效忽略动画?

您可以使用 DoubleAnimation 而不是 ObjectAnimationUsingKeyFrames。通过只设置它的 To 属性,而不是 From,动画从当前的 属性 值开始。还需要设置Border的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>