Unable to set property after animating it in WPF

I used this code to animate my window:

winLogin login = new winLogin();
login.Owner = this;
login.Show();

DoubleAnimation da = new DoubleAnimation();
da.From = 0;
da.To = this.Left + ((this.Width - login.Width) / 2);
da.AutoReverse = false;
da.Duration = new Duration(TimeSpan.FromSeconds(0.1));
login.BeginAnimation(Window.LeftProperty, da);

The problem is that whenever I set the property of Leftthis window (after the animation), it goes crazy.

I used this code to align the child windows always in the center, but the property Leftfor the windows on which I used the animation cannot be changed correctly.

private void Window_LocationChanged(object sender, EventArgs e)
{
        foreach (Window win in this.OwnedWindows)
        {
            win.Top = this.Top + ((this.Height - win.Height) / 2);
            win.Left = this.Left + ((this.Width - win.Width) / 2); 
        }
}
+3
source share
1 answer

First of all, when you set up an animation, you should always remove the potential previous animation of this property:

login.BeginAnimation(Window.LeftProperty, null);
login.BeginAnimation(Window.LeftProperty, da);

If you do not, you will get a memory leak and probably some other unwanted behavior.

- DependencyProperty DependecyProperty, , FillBehavior HoldEnd ( ). .

+5

Source: https://habr.com/ru/post/1765243/


All Articles