Search code examples
c#wpfanimationcolorscoloranimation

Wpf animate background color


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 be some other ways?

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

Solution

  • 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>