简体   繁体   English

按钮背景颜色动画

[英]Button Background Color Animate

I have around 30 buttons in my window and i want to change the background color of one of them after some processes. 我的窗口中大约有30个按钮,我想在进行一些处理后更改其中一个按钮的背景颜色。

Here's my code : 这是我的代码:

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);
}

Some of my buttons are attached to this event. 我的某些按钮已附加到该事件。 When this method runs and do it things, all the buttons with the same color as sender , begin the animation but i only want to sender runs the animation, not all of them. 当此方法运行并执行操作时,所有与sender颜色相同的按钮都将开始动画,但我只想让sender运行动画,而不是全部。 any solution to this ? 有什么解决办法吗? maybe i'm using a wrong property to begin the animation with. 也许我使用了错误的属性来开始播放动画。

Creating a new brush per button will solve your problem. 每个按钮创建一个新的画笔将解决您的问题。 The reason your buttons are sharing the animation is because class es are Reference Types in C#. 您的按钮共享动画的原因是因为class es是C#中的引用类型 When it is time to draw the buttons all buttons will notice the change the animation has made to the Color inside the brush they share. 是时候绘制按钮了,所有按钮都会注意到它们共享的笔刷中动画对“ Color所做的更改。 When you give each their own brush their brush will have their own color that can be animated independantly. 当您给每个画笔分配自己的画笔时,它们的画笔将具有自己的颜色,可以独立进行动画处理。 值和引用类型的内存表示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM