views:

464

answers:

1

Thanks to the answer on this stackoverflow question I was able to get the following animation to work, so that when the value of my ViewModel property PageToolBarVisible causes the toolbar to fade in and out.

The problem is:

  • the toolbar opacity fades out, but it the space it took up is still present after it fades out
  • the initial toolbar status is not in sync with the value of the ViewModel property

But how do I handle the following conditions, in XAML itself, if possible:

  • after the toolbar (Border) fades out, how do I then say "then and only then Visibility=Collapsed", (perhaps two animation happening at once or chained animations so visibile=collapsed happens after the first animation), edit: I since added the Trigger Opacity=0 below which works great
  • before the toolbar fades in, how do I say "Visibilty=Normal"
  • how do I also attach these events not only to the View Load process so that they show the correct status (faded in or faded out) when the page first appears?

Here is my animation so far:

    <Style x:Key="PageToolBarStyle" TargetType="Border">
        <Style.Triggers>
            <DataTrigger Binding="{Binding PageToolBarVisible}" Value="true">

                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation
                                Storyboard.TargetProperty="Opacity"
                                From="0.0" 
                                To="1.0" 
                                Duration="0:0:2"/>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>

                <DataTrigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation
                                Storyboard.TargetProperty="Opacity"
                                From="1.0" 
                                To="0.0" 
                                Duration="0:0:2"/>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.ExitActions>

            </DataTrigger>

            <Trigger Property="Opacity" Value="0">
                <Setter Property="Visibility" Value="Collapsed"/>
            </Trigger>

        </Style.Triggers>
    </Style>

<Window.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        <EventTrigger.Actions>
            ...how can I tell it here to "do the trigger logic contained in "PageToolBarStyle"...
        </EventTrigger.Actions>
    </EventTrigger>
</Window.Triggers>
+2  A: 

You could create a trigger for the opacity on the element in question. Once that is "0" you can then set the Visibility of the element. This should handle both the first and second point from above.

As for the third point ... if I understand you correctly, then there's a FrameworkElement.Loaded event that you can use to solve the problem of the running the animation when the View is first loaded.

Chris Nicol
FraneworkElement.Loaded should be part of the Window.Triggers. As for the opacity trigger, I'm not sure why that isn't working. An alternative is to add a method to the completed event to set the visibility in code-behind (but I get that you want to keep it in the XAML)
Chris Nicol
I added both suggestions, the first one (trigger opacity=0) works great, but the second (frameworkelement.loaded) causes an error, where does it need to go exactly?
Edward Tanguay
OK, I put <EventTrigger RoutedEvent="FrameworkElement.Loaded"> in Windows.Triggers as shown above but how do I then tell it to "do the trigger logic contained in "PageToolBarStyle" at that point?
Edward Tanguay
If you create your storyboard as a resource ... then you can just call the resource.<EventTrigger.Actions> <BeginStoryboard Storyboard="{StaticResource NamedStoryboardResource}" /></EventTrigger.Actions>
Chris Nicol
But I don't want to call the StoryBoard FadeIn or the StoryBoard FadeOut, I want to call the STYLE so that it executes this logic <DataTrigger Binding="{Binding PageToolBarVisible}" Value="true"> and then ITSELF appropriately chooses which StoryBoard to execute (the ViewModel property may be true or false upon starting).
Edward Tanguay
I asked this question more succinctly here: http://stackoverflow.com/questions/1092330/how-can-i-make-one-trigger-call-another-so-as-to-not-repeat-xaml-trigger-code
Edward Tanguay