WPF:如何动画颜色变化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4565464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:27:44  来源:igfitidea点击:

WPF: How to animate color change?

wpfanimationcoloranimation

提问by Boris

I have a grid, a window root element. I want to apply an animation which would change it's background color from white to green in 5 seconds. Here's what I did:

我有一个网格,一个窗口根元素。我想应用一个动画,它会在 5 秒内将其背景颜色从白色变为绿色。这是我所做的:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    ColorAnimation animation;

    animation = new ColorAnimation();
    animation.From = Colors.White;
    animation.To = Colors.Green;
    animation.Duration = new Duration(TimeSpan.FromSeconds(5));
    rootElement.BeginAnimation(Grid.BackgroundProperty, animation);
}

The code doesn't work. Nothing is changing. Where am I making a mistake? Thanks.

该代码不起作用。什么都没有改变。我在哪里犯了错误?谢谢。

回答by Boris

Solved!

解决了!

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    SolidColorBrush rootElementBrush;
    ColorAnimation animation;

    rootElementBrush = this.FindResource("RootElementBrush") as SolidColorBrush;

    // Animate the brush 
    animation = new ColorAnimation();
    animation.To = Colors.Green;
    animation.Duration = new Duration(TimeSpan.FromSeconds(5));
    rootElementBrush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
}

Here's an explanation:

这是一个解释:

My initial mistake was that I wanted to change the Grid.BackgroundPropertyby assigning colors to it, but it accepts brushes instead... apples and oranges! So, I created a SolidColorBrushstatic resource and named it rootElementBrush. In XAML, I set Grid rootElement's background property to that static resource. And finally, I modified the animation, so now it changes the color for that SolidColorBrush. Easy!

我最初的错误是我想Grid.BackgroundProperty通过为它分配颜色来改变它,但它接受画笔而不是......苹果和橙子!因此,我创建了一个SolidColorBrush静态资源并将其命名为 rootElementBrush。在 XAML 中,我将Grid rootElement的背景属性设置为该静态资源。最后,我修改了动画,所以现在它改变了那个的颜色SolidColorBrush。简单!

回答by THE DOCTOR

Give this a try:

试试这个:

<ColorAnimation
Storyboard.TargetName="PlayButtonArrow" 
Storyboard.TargetProperty="Fill.Color"
From="White"
To="Green"              
Duration="0:0:5.0"
AutoReverse="False"/>

回答by Michal Heczko

You do not need to set the StaticResource, just use the Storyboard.

您不需要设置StaticResource,只需使用Storyboard

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    // Animate the brush 
    ColorAnimation animation = new ColorAnimation();
    animation.To = Colors.Green;
    animation.Duration = new Duration(TimeSpan.FromSeconds(5));
    Storyboard.SetTargetProperty(animation, new PropertyPath("(Grid.Background).(SolidColorBrush.Color)", null));
    Storyboard storyboard = new Storyboard();
    storyboard.Children.Add(animation);
    storyboard.Begin(rootElement);
}