1

So i have StackPanel that i want to show with blink Style for several seconds and after that i want it to disappear.

I do not want it to be Automatically but control it from code behind:

So currently this is what i have so far:

<Style x:Key="FaderStyle" TargetType="{x:Type StackPanel}">
            <Style.Resources>
                <Storyboard x:Key="FadeStoryboard">
                    <DoubleAnimation Storyboard.TargetProperty="(StackPanel.Opacity)" 
                                     From="0"
                                     To="1" Duration="0:0:0.7"
                                     RepeatBehavior="0:0:5"
                                     AutoReverse="True"/>
                </Storyboard>
            </Style.Resources>
            <Style.Triggers>
                <Trigger Property="Visibility" Value="Visible">
                    <Trigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource FadeStoryboard}"/>
                    </Trigger.EnterActions>
                </Trigger>
            </Style.Triggers>
        </Style>

Code behind:

StackPanel sp;
Storyboard storyboard = Resources["FaderStyle"] as Storyboard;
            if (storyboard != null)
                storyboard.Begin(sp);

So currently my StackPanel Visibility is Collapsed and after i start the Animation i still cannot see it.

Clemens
  • 123,504
  • 12
  • 155
  • 268
falukky
  • 1,099
  • 2
  • 14
  • 34

1 Answers1

0

Your code is fine. But your approach to starting the animation is wrong. The trigger starts the animation when Visibility changes to Visible. Not the other way around (which your last piece of code indicates) starting the animation does not change visibility because you didn't write the logic to do that

So with your given code, you need to change the visibility in order to start the animation:

StackPanel sp;
sp.Visibility = Visibility.Visible;

Note that the animation only starts when it enters Visible state. Which means you need to make it collapsed or hidden first.

Bizhan
  • 16,157
  • 9
  • 63
  • 101