1

I can not animate height and width of window together with top and left property.
(If you want to run this, link to download example is at the end of this question.)

Always when I run this animation, my window firstly smooth move to new location (change smoothly top and left) and also smoothly animate width property during window move.. but window height not responds.. then window height jumps from one value (old height) to the new height.

My WPF application is easy - only for testing this (4 properties, 1 button and 1 function):

Firstly I added this properties to my window (file: MainWindow.xaml.cs):

    /// <summary>
    /// Position to lock for change size in horizontal way (x)
    /// </summary>
    public static readonly DependencyProperty LocationLockHorizontalProperty = DependencyProperty.Register("LocationLockHorizontal", typeof(AlignmentX), typeof(Window));
    /// <summary>
    /// Position to lock for change size in vertical way (y)
    /// </summary>
    public static readonly DependencyProperty LocationLockVerticalProperty = DependencyProperty.Register("LocationLockVertical", typeof(AlignmentY), typeof(Window));
    /// <summary>
    /// Location to lock for change size.
    /// </summary>
    public AlignmentX LocationLockHorizontal
    {
        get { return (AlignmentX)GetValue(LocationLockHorizontalProperty); }
        set { SetValue(LocationLockHorizontalProperty, value); }
    }
    /// <summary>
    /// Location to lock for change size.
    /// </summary>
    public AlignmentY LocationLockVertical
    {
        get { return (AlignmentY)GetValue(LocationLockVerticalProperty); }
        set { SetValue(LocationLockVerticalProperty, value); }
    }

Then I added to the same file one new procedure for create and start animation:

public void AnimateResize(double changeWidth = 0d, double changeHeight = 0d, double durationMilisec = 200.0)
{
    Storyboard sb = new Storyboard();

    DoubleAnimation daw;
    DoubleAnimation dah;

    // animate window width
    if (changeWidth != 0.0)
    {
        daw = new DoubleAnimation();
        daw.From = this.ActualWidth;
        daw.To = this.ActualWidth + changeWidth;
        daw.Duration = new Duration(TimeSpan.FromMilliseconds(durationMilisec));
        daw.AccelerationRatio = 0.4;
        daw.DecelerationRatio = 0.6;
        // this.BeginAnimation(Window.WidthProperty, daw);
        Storyboard.SetTarget(daw, this);
        Storyboard.SetTargetProperty(daw, new PropertyPath(Window.WidthProperty));
        sb.Children.Add(daw);
    }

    // animate window height
    if (changeHeight != 0.0)
    {
        dah = new DoubleAnimation();
        dah.From = this.ActualHeight;
        dah.To = this.ActualHeight + changeHeight;
        dah.Duration = new Duration(TimeSpan.FromMilliseconds(durationMilisec));
        dah.AccelerationRatio = 0.4;
        dah.DecelerationRatio = 0.6;
        Storyboard.SetTarget(dah, this);
        Storyboard.SetTargetProperty(dah, new PropertyPath(Window.HeightProperty));
        sb.Children.Add(dah); // this.BeginAnimation(Window.HeightProperty, dah);
    }

    DoubleAnimation dax;
    DoubleAnimation day;

    // animate window move in horizontal way
    if (LocationLockHorizontal == AlignmentX.Center || LocationLockHorizontal == AlignmentX.Right)
    {
        dax = new DoubleAnimation();
        dax.From = this.Left;
        switch (LocationLockHorizontal)
        {
            case AlignmentX.Center:
                dax.To = this.Left - changeWidth / 2.0;
                break;
            case AlignmentX.Right:
                dax.To = this.Left - changeWidth;
                break;
        }
        dax.Duration = new Duration(TimeSpan.FromMilliseconds(durationMilisec));
        dax.AccelerationRatio = 0.4; dax.DecelerationRatio = 0.6;

        Storyboard.SetTarget(dax, this);
        Storyboard.SetTargetProperty(dax, new PropertyPath(Window.LeftProperty));
        sb.Children.Add(dax); // this.BeginAnimation(Window.LeftProperty, dax);
    }

    // animate window move vertical 
    if (LocationLockVertical == AlignmentY.Center || LocationLockVertical == AlignmentY.Bottom)
    {
        day = new DoubleAnimation();
        day.From = this.Top;

        switch (LocationLockVertical)
        {
            case AlignmentY.Center:
                day.To = this.Top - changeHeight / 2.0;
                break;
            case AlignmentY.Bottom:
                day.To = this.Top - changeHeight;
                break;
        }
        day.Duration = new Duration(TimeSpan.FromMilliseconds(durationMilisec));
        day.AccelerationRatio = 0.4; day.DecelerationRatio = 0.6;

        Storyboard.SetTarget(day, this);
        Storyboard.SetTargetProperty(day, new PropertyPath(Window.TopProperty));
        sb.Children.Add(day); // this.BeginAnimation(Window.TopProperty, day);
    }
    sb.Begin();
}

In MyWindow initialization in the same file MainWindow.xaml.cs, properties are set to this values (to fix animation to bottom point vertically and center horizontally):

public MainWindow()
{
    InitializeComponent();
    LocationLockHorizontal = AlignmentX.Center; // fix center point when animate resizing
    LocationLockVertical = AlignmentY.Bottom; // lock bottom corner of application
}

My application has 1 button and for it I set click event for random call of previous procedure:

private void Button_Click(object sender, RoutedEventArgs e)
{
    this.AnimateResize(new Random().NextDouble() * 400.0 - 200.0, new Random().NextDouble() * 500.0 - 250.0, 1000.0);
}

For complete example here is my xaml MainWindow.xaml:

<Window x:Class="Test_HeightAndTopAnimation.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="300" WindowStartupLocation="CenterScreen">
  <Grid>
    <Button Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
  </Grid>
</Window>

I read several articles (Wpf - Animate height from bottom up - I don`t have a grid, or WPF: Animation is not smooth) but nothing looks related to this problem.

Link to zip file with example: Application code on Google Drive

Only last note: I use Windows7 64bit and .NET 4.5

Community
  • 1
  • 1
Atiris
  • 2,613
  • 2
  • 28
  • 42
  • Yeah, this is a weird one. If you add the `Height` animation into the `Storyboard.Children` collection first, then the `Height` animates smoothly and the `Width` animation stutters. – Sheridan Jun 10 '14 at 12:24
  • 1
    It seems the problem is in Window class. You can't animate its Width and Height properties simultaneously. Here is a [workaround](http://stackoverflow.com/questions/12332385/animating-a-wpf-window-width-and-height). – undermind Jun 10 '14 at 16:58

0 Answers0