我目前正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.

推荐答案

这些控件使用单个共享背景画笔,您可以为其的 colored颜色 属性设置动画.

将单独的SolidColorBrush分配给每个应设置动画的控件:

<Border x:Name="target">
    <Border.Background>
        <SolidColorBrush Color="White"/>
    </Border.Background>
</Border>

Csharp相关问答推荐

Unity中的Obi Fluid-改变解算器重力方向对粒子没有影响

火鸟DBC驱动程序未加载ADF DLC

以自动方式注销Azure身份应用程序

如何在Reflection. Emit中使用具有运行时定义的类型参数的泛型类型

为什么C#Bigbit不总是相同的比特长度?

当我使用NET6作为目标框架时,为什么DotNet使用NET8作为MS包?

将XPS转换为PDF C#

Azure Redis缓存与Entra ID身份验证

在调整大小的控件上绘制

.NET SDK包中的官方C#编译器在哪里?

正确处理嵌套的本机集合

TagHelpers在新区域不起作用

该函数不能检测两条曲线的交点

.NET 8 DI GetServices<;对象&>不工作

在implementationFactory中避免循环依赖

C#动态设置ServerReport报表参数

我什么时候应该在Dapper中使用Connection.OpenAsync?

在C#中,当输入一个方法/局部函数时,我的IEnumerator被重置/重新创建.为什么?

C#-如何将int引用获取到byte[]

如何使ExecuteAsync异步运行