Animating both values at the same time

Is it possible to implement resize window of two values “height”, “width” in one moment? Without waiting for the action of one and after the other? :PaimonNomming:
private void ResizeWindow(double width, double height)
{
Duration duration = TimeSpan.FromSeconds(2);

DoubleAnimation widthAnimation = new DoubleAnimation();
widthAnimation.To = width;
widthAnimation.Duration = duration;

DoubleAnimation heightAnimation = new DoubleAnimation();
heightAnimation.To = height;
heightAnimation.Duration = duration;

Storyboard storyboard = new Storyboard();
storyboard.Children.Add(widthAnimation);
storyboard.Children.Add(heightAnimation);
Storyboard.SetTarget(widthAnimation, this);
Storyboard.SetTarget(heightAnimation, this);
Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(Window.WidthProperty));
Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Window.HeightProperty));

storyboard.Begin();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
ResizeWindow(640, 360);
}
private void ResizeWindow(double width, double height)
{
Duration duration = TimeSpan.FromSeconds(2);

DoubleAnimation widthAnimation = new DoubleAnimation();
widthAnimation.To = width;
widthAnimation.Duration = duration;

DoubleAnimation heightAnimation = new DoubleAnimation();
heightAnimation.To = height;
heightAnimation.Duration = duration;

Storyboard storyboard = new Storyboard();
storyboard.Children.Add(widthAnimation);
storyboard.Children.Add(heightAnimation);
Storyboard.SetTarget(widthAnimation, this);
Storyboard.SetTarget(heightAnimation, this);
Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(Window.WidthProperty));
Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Window.HeightProperty));

storyboard.Begin();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
ResizeWindow(640, 360);
}
1 Reply
leowest
leowest2mo ago
There is one hack around it but unsure if that would be of your interest, which is setting the animation to the top level grid after the window and setting in the window settings SizeToContent="WidthAndHeight" then it animates both
<Grid x:Name="MyGrid" Height="1000" Width="1000">
<Grid x:Name="MyGrid" Height="1000" Width="1000">
and your code
private void ResizeWindow(double width, double height)
{
Duration duration = TimeSpan.FromSeconds(2);

DoubleAnimation widthAnimation = new DoubleAnimation();
widthAnimation.To = width;
widthAnimation.Duration = duration;

DoubleAnimation heightAnimation = new DoubleAnimation();
heightAnimation.To = height;
heightAnimation.Duration = duration;

Storyboard storyboard = new Storyboard();
storyboard.Children.Add(widthAnimation);
storyboard.Children.Add(heightAnimation);
Storyboard.SetTarget(widthAnimation, MyGrid);
Storyboard.SetTarget(heightAnimation, MyGrid);
Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(Grid.WidthProperty));
Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Grid.HeightProperty));

storyboard.Begin();
}
private void ResizeWindow(double width, double height)
{
Duration duration = TimeSpan.FromSeconds(2);

DoubleAnimation widthAnimation = new DoubleAnimation();
widthAnimation.To = width;
widthAnimation.Duration = duration;

DoubleAnimation heightAnimation = new DoubleAnimation();
heightAnimation.To = height;
heightAnimation.Duration = duration;

Storyboard storyboard = new Storyboard();
storyboard.Children.Add(widthAnimation);
storyboard.Children.Add(heightAnimation);
Storyboard.SetTarget(widthAnimation, MyGrid);
Storyboard.SetTarget(heightAnimation, MyGrid);
Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(Grid.WidthProperty));
Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Grid.HeightProperty));

storyboard.Begin();
}