【发布时间】:2019-05-16 17:03:16
【问题描述】:
我的窗口中有大约 30 个按钮,我想在经过一些处理后更改其中一个按钮的背景颜色。
这是我的代码:
private void Button_Click(object sender, RoutedEventArgs e){
.
. //A Dialog Shows Up And Do Some Processes
.
Button b = (Button)sender;
ColorAnimation ca = new ColorAnimation();
ca.From = ((SolidColorBrush)b.Background).Color;
ca.To = Color.FromArgb(255, 132, 27, 13);
ca.Duration = new Duration(TimeSpan.FromMilliseconds(2000));
ca.EasingFunction = new QuadraticEase();
b.Background.BeginAnimation(SolidColorBrush.ColorProperty, ca);
}
我的一些按钮已附加到此事件。当此方法运行并执行此操作时,与sender 具有相同颜色的所有按钮开始动画,但我只想sender 运行动画,而不是所有按钮。有什么解决办法吗?也许我使用了错误的属性来开始动画。
【问题讨论】:
-
将不同的 SolidColorBrush 实例添加到按钮的背景中。每个按钮一个。请注意,您不需要设置
ca.From。它会自动从当前值开始。 -
@Clemens 感谢您的解决方案,工作正常,我对所有按钮都使用了一个画笔,我将其更改为
button.background = mybrush.Clone();并修复了问题
标签: c# wpf animation button wpf-controls