Matrix
Matrix

Reputation: 123

WPF Animation (changing multiple properties of single element at the same time)

I need to animate multiple properties of one ui element at the same time.

For example, decreasing width and height of windows synchronously.

Any idea?

        DoubleAnimation widthAnimation = new DoubleAnimation
        {           
            To = 0,
            Duration = TimeSpan.FromSeconds(5)
        };

        DoubleAnimation heightAnimation = new DoubleAnimation
        {
            To = 0,
            Duration = TimeSpan.FromSeconds(5)
        };

        Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(Window.WidthProperty));
        Storyboard.SetTarget(widthAnimation, this);

        Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Window.HeightProperty));
        Storyboard.SetTarget(heightAnimation, this);

        Storyboard s = new Storyboard();
        s.Completed += FadeOut_Completed;
        s.Children.Add(widthAnimation);
        s.Children.Add(heightAnimation);
        this.BeginStoryboard(s, HandoffBehavior.SnapshotAndReplace, true);    

It will do animations step by step; height will change after width changes are complete! :|

Upvotes: 2

Views: 3782

Answers (1)

pushpraj
pushpraj

Reputation: 13679

After looking at your code I understand that you are trying to animate the width and height of Window simultenousely

But I would regret to tell you that since window is not an actual wpf component but a platform component. however content of window is completely controllable as expected via your code, but window is not. any such changes are routed through Pinvoke. and the issue you are facing is a known issue and the work around are bit complex

one solution is here, this uses pinvoke to animate the window's height and width

Animating a WPF window width and height

here is a bug for similar issue created at microsoft, result is (Closed, as Won't Fix)

https://connect.microsoft.com/VisualStudio/feedback/details/715415/window-width-height-animation-in-wpf-got-broken-on-net-framework-4-0

Extra

below is a sample which is not actually solving your problem but will help you to reduce the number of lines you need to perform such animations for other elements. It is a rewrite of your code in less lines

        DoubleAnimation anim = new DoubleAnimation
        {
            To = 0,
            Duration = TimeSpan.FromSeconds(5)
        };

        border.BeginAnimation(Border.HeightProperty, anim);
        border.BeginAnimation(Border.WidthProperty, anim);

try this code with any element except window, I used a border with some color filled

apologies for overlooking the Window in your code at first sight

Upvotes: 2

Related Questions