WPF How to skip animation to end if a boolean value in ViewModel is true?

1k views Asked by At

I have a screen with animation in my WPF application. I need the animation to run only onceper user session. This means that if a user sees the screen with the animation the first time, it would play, but when the user comes back to it it will be in its final state (skipped to end).

I have a boolean value in my ViewModel which holds all the users/sessions logic and it indicates weather or not the animation should run or it should be shown at its final frame.

How can i achieve this type of Binding / functionality only with XAML?

2

There are 2 answers

2
Florian Gl On

An easy solution would be to bind your bool Prop to the Duration-property of the Binding.

Now you need a converter which converts bool to TimeSpan, like so:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    bool showAnimation = (bool)value;
    return (showAnimation ) ? new TimeSpan(0,0,5) : new TimeSpan(0,0,0);
}

EDIT: With SkipStoryboardToFill mentioned in Sheridan's answer, you could do something like:

            <DataTrigger Binding="{Binding Path=EnableStoryboard}" Value="False">
                <DataTrigger.EnterActions>
                    <SkipStoryboardToFill BeginStoryboardName="BeginStoryboard"></SkipStoryboardToFill>
                </DataTrigger.EnterActions>
            </DataTrigger>

But this only will work if you change the bool property while the BeginStoryboard is running.

4
Sheridan On

You might like to take a look at the SkipStoryboardToFill Class page on MSDN for your answer. There is a great example on the linked page that fully explains how to manipulate a Storyboard object. From the linked example:

<EventTrigger RoutedEvent="Button.Click" SourceName="SkipToFillButton">
    <SkipStoryboardToFill BeginStoryboardName="MyBeginStoryboard" />
</EventTrigger>