无法在WPF中设置动画后设置属性
我使用以下代码为我的窗口设置动画:无法在WPF中设置动画后设置属性,wpf,animation,Wpf,Animation,我使用以下代码为我的窗口设置动画: 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(Ti
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);
问题是,每当我设置此窗口的Left
属性(在动画之后),它都会变得疯狂
我使用此代码使子窗口始终居中对齐,但无法正确更改使用动画的窗口的Left
属性
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);
}
}
首先,设置动画时,应始终删除该属性的潜在上一个动画:
login.BeginAnimation(Window.LeftProperty, null);
login.BeginAnimation(Window.LeftProperty, da);
如果你不这样做,你会得到一个内存泄漏和可能的其他一些不希望的行为
此外,由于无法在具有活动动画的DependecProperty上设置值,动画中就是这种情况,因为其设置为
HoldEnd
(默认设置)。同样,您必须先删除动画。实际上,使用SetCurrentValue设置值不是很好吗?当然,动画仍将优先于HoldEnd。新的.NET 4 DependencyObject.SetCurrentValue用于设置数据绑定DependencyProperties上的值,而不影响数据绑定源值。但是,Window.LeftProperty不是数据绑定的。动画直接设置属性。