Note that there are some explanatory texts on larger screens.

plurals
  1. POAnimating a WPF window width and height
    text
    copied!<p>I'd like to animate the width and height of a wpf window. I've tried the following, which unfortunately just animates the width... the height of the window never changes.</p> <p>I'm sure I've missed something silly and hope that by posting here someone will see my error!</p> <p>Here's the code behind for a simple window with a button I've wired up do to the resize:</p> <pre><code>public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { this.AnimateWindowSize(ActualWidth + 200, ActualHeight + 200); } } </code></pre> <p>And here is the animation code I've written as an extension method so it could be applied to any window...</p> <pre><code>public static class WindowUtilties { public static void AnimateWindowSize(this Window target, double newWidth, double newHeight) { var sb = new Storyboard {Duration = new Duration(new TimeSpan(0, 0, 0, 0, 200))}; var aniWidth = new DoubleAnimationUsingKeyFrames(); var aniHeight = new DoubleAnimationUsingKeyFrames(); aniWidth.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 200)); aniHeight.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 200)); aniHeight.KeyFrames.Add(new EasingDoubleKeyFrame(target.ActualHeight, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 00)))); aniHeight.KeyFrames.Add(new EasingDoubleKeyFrame(newHeight, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 200)))); aniWidth.KeyFrames.Add(new EasingDoubleKeyFrame(target.ActualWidth, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 00)))); aniWidth.KeyFrames.Add(new EasingDoubleKeyFrame(newWidth, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 200)))); Storyboard.SetTarget(aniWidth, target); Storyboard.SetTargetProperty(aniWidth, new PropertyPath(Window.WidthProperty)); Storyboard.SetTarget(aniHeight, target); Storyboard.SetTargetProperty(aniHeight, new PropertyPath(Window.HeightProperty)); sb.Children.Add(aniWidth); sb.Children.Add(aniHeight); sb.Begin(); } } </code></pre> <p>Thanks in advance for any help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload