Wpf 动画背景色 [英] Wpf animate background color

查看:68
本文介绍了Wpf 动画背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助才能做出正确的决定.当某些事件发生时,我需要为我的用户控件的背景颜色设置动画.如果是,我只想将背景更改 1 秒钟,然后再将其转回来.我应该走哪条路?使用彩色动画或计时器,也可以通过其他方式.

I need help in taking right decision. I need to animate a background color of my user control when some event happens. When it is, I want to change the background just for 1 second and then turn it back. Which way should I go? Use color animation or timer or may by some other way.

解决了.谢谢大家!这对我很有用:

Solved. Thanks to all! This works good for me:

        ColorAnimation animation;
        animation = new ColorAnimation();
        animation.From = Colors.Orange;
        animation.To = Colors.Gray;
        animation.Duration = new Duration(TimeSpan.FromSeconds(1));
        this.elGrid.Background.BeginAnimation(SolidColorBrush.ColorProperty, animation);

推荐答案

我会使用 EventTriggerColorAnimation.

在本例中,Button BrackgroundMouseLeave 事件上变为绿色.希望此代码与您可能需要的类似.

In this example a Button Brackground goes green on a MouseLeave event. This code is hopefully similar to what you may need.

<Button Content="Button" Height="75" HorizontalAlignment="Left" Margin="27,12,0,0" Name="btnImgBrush" VerticalAlignment="Top" Width="160" Background="LightGray">
    <Button.Triggers>
        <EventTrigger RoutedEvent="Button.MouseLeave">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation To="Green" 
                                    Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" 
                                    FillBehavior="Stop" 
                                    Duration="0:0:1"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Button.Triggers>
</Button>

这篇关于Wpf 动画背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆