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

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

问题描述

我需要帮助作出正确的决定。当一些事件发生时,我需要为我的用户控件的背景颜色设置动画。当它是,我想改变背景只有1秒,然后把它回来。我应该选择哪种方式?使用颜色动画或计时器,或者可以通过其他方式。



解决。谢谢大家!这对我有好处:

  ColorAnimation动画; 
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);


解决方案

我会使用 EventTrigger c> c。


$ b 在此示例中,按钮Brackground MouseLeave 事件上变为绿色。这个代码希望类似于你可能需要的。

 < Button Content =Button Height =75Horizo​​ntalAlignment =LeftMargin =27,12,0,0Name =btnImgBrushVerticalAlignment =TopWidth =160Background =LightGray> 
< Button.Triggers>
< EventTrigger RoutedEvent =Button.MouseLeave>
< BeginStoryboard>
< Storyboard>
< ColorAnimation To =Green
Storyboard.TargetProperty =(Button.Background)。(SolidColorBrush.Color)
FillBehavior =Stop
Duration = :0:1/>
< / storyboard>
< / BeginStoryboard>
< / EventTrigger>
< /Button.Triggers>
< / Button>


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

解决方案

I would use an EventTrigger with a ColorAnimation.

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天全站免登陆