WPF routedevent storyboard begin only if height is zero

120 views Asked by At

I have the following XAML for a border trigger that uses a routed event

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

This trigger fires when the border and containing elements are clicked and animates cause the target to animate open from height of Zero to 200

The trigger works really well but each time the border receives the event the animation runs and the target animates open again (even if already open)

How can one add a condition to the trigger that effectively ignores the animation is the target already has a height greater than Zero?

1

There are 1 answers

0
Clemens On BEST ANSWER

You may use a DoubleAnimation instead of an ObjectAnimationUsingKeyFrames. By only setting its To property, but not From, the animation starts from the current property value. It requires that you also set an initial value of the Height of the Border:

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