2

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Uri Abramson
  • 6,005
  • 6
  • 40
  • 62

2 Answers2

2

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.

Florian Gl
  • 5,984
  • 2
  • 17
  • 30
  • The user asked for a XAML solution... luckily, I'm not one of those idiots that loves to down vote for the slightest reasons. – Sheridan Sep 11 '13 at 10:14
  • Well.. luckily i already have in my code a SwitchCase Covnerter so i guess i could use that without adding any new code... – Uri Abramson Sep 11 '13 at 10:39
2

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>
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • I need to bind this to a boolean and the boolean should override the BeginStoryboard event. So that's no good for me but thank – Uri Abramson Sep 11 '13 at 10:38
  • Good luck then... *or* you could just create your own `RoutedEvent` and raise it whenever you would set your `Boolean` property. – Sheridan Sep 11 '13 at 10:38
  • WPF sucks. Way too much plumbing – Uri Abramson Sep 11 '13 at 10:39
  • 3
    I couldn't disagree more... it's all that 'plumbing' as you call it that makes it so versatile. If you don't like, don't use it... you could go back to using crusty old WinForms instead. :) – Sheridan Sep 11 '13 at 10:42