Search code examples
c#wpfanimationmainwindow

How to animate the MainWindow itself when it is loaded


I am fairly new to wpf.I want to apply a fade animation on the MainWindow as soon as it is displayed on the screen.Is that possible?Please Help me out


Solution

  • You can apply StoryBoard on window loaded event to give a fade look to your window. This should work for you -

    <Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="(Window.Opacity)"
                                     From="0.0" To="1.0" Duration="0:0:1"
                                     AutoReverse="False"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>
    

    Also you can do it in code behind in window loaded event handler if you don't want to do it in XaML -

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DoubleAnimation animation = new DoubleAnimation(0, 1, 
                                        (Duration)TimeSpan.FromSeconds(1));
            this.BeginAnimation(UIElement.OpacityProperty, animation);
        }