我目前正try 在我的WPF应用程序中设置大约Border
个元素的动画.
我需要更改Background
个属性,并将ColorAnimation
类与此助手方法一起使用:
public static void AnimateColor(SolidColorBrush to, Animatable fe)
{
ColorAnimation ca = new()
{
To = to.Color,
Duration = new(TimeSpan.FromSeconds(0.5)),
};
fe.BeginAnimation(SolidColorBrush.ColorProperty, ca);
}
我使用的方法如下:
AnimateColor(new SolidColorBrush(Colors.Red), target.Background);
target是我要设置动画效果的控件.
然而,当我运行代码时,相同控件类型的all instances个元素是动画的,例如,当前窗口中的所有Border
个元素都是动画的.
我错过了什么吗?如何从代码隐藏中为Control的single实例设置动画?
事先感谢您的帮助.
Edit, solved by accepted answer个
我正在更新我的答案,这样任何被这个"问题"困扰的人都可以通过首先了解WPF如何处理动画来理解它.
正如接受答案所建议的,我 for each 边框背景设置了一个初始值,如下所示:
SolidColorBrush initialBackground = new(Colors.White);
border1.Background = initialBackground;
border2.Background = initialBackground;
border3.Background = initialBackground;
我以为动画可以在控件上运行,但这是我的错误.
现在我明白了,WPF ColorAnimation
是在为DependecyProperty设置动画的特定值上工作的,所以在我的例子中,我总是设置相同的实例InitialBackground.