【发布时间】:2023-03-18 21:00:02
【问题描述】:
大家,我尝试缩放按钮的大小,代码工作正常,如果我只单击一次,但如果我连续单击多次按钮,按钮无法返回其原始大小。 这是代码:
private void ButtonSearchMedication_OnClick(object sender, RoutedEventArgs e)
{
//Assign variation of width in term of begin, end and duration
DoubleAnimation widthAnimation =new DoubleAnimation(ButtonSearchMedication.ActualWidth, ButtonSearchMedication.ActualWidth*0.8, new Duration(timeSpan:TimeSpan.FromSeconds(0.2)) );
//Assign variation of height in term of begin, end and duration
DoubleAnimation heightAnimation = new DoubleAnimation(ButtonSearchMedication.ActualHeight,ButtonSearchMedication.ActualHeight*0.8, new Duration(timeSpan:TimeSpan.FromSeconds(0.2)));
//Assign properties to button
ButtonSearchMedication.BeginAnimation(Button.WidthProperty,widthAnimation);
ButtonSearchMedication.BeginAnimation(Button.HeightProperty,heightAnimation);
}
private void ButtonSearchMedication_OnMouseLeave(object sender, MouseEventArgs e) {
DoubleAnimation widthAnimation = new DoubleAnimation(ButtonSearchMedication.ActualWidth, ButtonSearchMedication.ActualWidth*1.25,new Duration(timeSpan:TimeSpan.FromSeconds(0.2)));
DoubleAnimation heightAnimation = new DoubleAnimation(ButtonSearchMedication.ActualHeight, ButtonSearchMedication.ActualHeight*1.25,new Duration(timeSpan:TimeSpan.FromSeconds(0.2)));
ButtonSearchMedication.BeginAnimation(Button.WidthProperty,widthAnimation);
ButtonSearchMedication.BeginAnimation(Button.HeightProperty,heightAnimation);
}
我可以做些什么来确保按钮的大小在每次 MouseLeave 后变为原来的大小?谢谢
【问题讨论】:
-
问题是你同时使用
from和to。我不确定是否可以通过删除任何一个来修复,但是您始终可以在单击开始动画之前保存 original 大小(并检查动画是否已经在运行以不覆盖这些),然后您将能够在鼠标离开事件中恢复它。 -
嗨,Sinatr,我的意思是,如果我点击 n 次,大小将为 0.8^n。我正在寻找一个逻辑(如 if()..)来确定按钮的大小何时恢复到原始
标签: c# wpf doubleanimation