Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I have the following problem : I want to create a animation on a button that changes the brush (linear gradient brush) of a button. The button is initially gray and when the mouse is over it, I want to start from the left and change the color to blue smoothly until the whole button is blue. But I don't want it to stop it here but after that to repeat the behavior but with another color randomly, to create a waterfall like effect. The problem is how to bound the To property of the Color animation to a collection of color and after each complete animation to change to the next item in the collection. And I would like to do this in XAML not programmatically.
Is there any answer to this problem?

Thanks in advance,
Tamash
Posted

Have you looked at ColorAnimation? That's precisely meant for this.

http://msdn.microsoft.com/en-us/library/system.windows.media.animation.coloranimation.aspx[^]
 
Share this answer
 
Hi!

Seems it is exactly what you need

XML
<Button Content="Load" Width="100" Height="30" Click="Button_Click">
            <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Border Background="{TemplateBinding Background}">
                            <ContentControl Content="{TemplateBinding Content}"
                                            VerticalAlignment="Center"
                                            HorizontalAlignment="Center"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Button.Template>
            <Button.Triggers>
                <EventTrigger RoutedEvent="MouseLeave">
                    <BeginStoryboard>
                        <Storyboard Duration="0:0:3">
                            <ColorAnimation Storyboard.TargetName="stop1" Storyboard.TargetProperty="Color" To="LightGray"/>
                            <ColorAnimation Storyboard.TargetName="stop2" Storyboard.TargetProperty="Color" To="LightGray"/>
                            <ColorAnimation Storyboard.TargetName="stop3" Storyboard.TargetProperty="Color" To="LightGray"/>
                            <ColorAnimation Storyboard.TargetName="stop4" Storyboard.TargetProperty="Color" To="LightGray"/>
                            <ColorAnimation Storyboard.TargetName="stop5" Storyboard.TargetProperty="Color" To="LightGray"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="MouseEnter">
                    <BeginStoryboard>
                        <Storyboard Duration="0:0:3">
                            <ColorAnimation Storyboard.TargetName="stop1" Storyboard.TargetProperty="Color" To="LightBlue"/>
                            <ColorAnimation Storyboard.TargetName="stop2" Storyboard.TargetProperty="Color" To="DarkBlue"/>
                            <ColorAnimation Storyboard.TargetName="stop3" Storyboard.TargetProperty="Color" To="LightBlue"/>
                            <ColorAnimation Storyboard.TargetName="stop4" Storyboard.TargetProperty="Color" To="DarkBlue"/>
                            <ColorAnimation Storyboard.TargetName="stop5" Storyboard.TargetProperty="Color" To="LightBlue"/>
                        </Storyboard>
                    </BeginStoryboard>
                    <BeginStoryboard>
                        <Storyboard Duration="0:0:3" RepeatBehavior="Forever">
                            <DoubleAnimation  Storyboard.TargetName="stop1" Storyboard.TargetProperty="Offset" To="0"
                                              From="-1" RepeatBehavior="Forever"/>
                            <DoubleAnimation  Storyboard.TargetName="stop2" Storyboard.TargetProperty="Offset" To="0.5"
                                              From="-0.5" RepeatBehavior="Forever"/>
                            <DoubleAnimation  Storyboard.TargetName="stop3" Storyboard.TargetProperty="Offset" To="1"
                                              From="0" RepeatBehavior="Forever"/>
                            <DoubleAnimation  Storyboard.TargetName="stop4" Storyboard.TargetProperty="Offset" To="1.5"
                                              From="0.5" RepeatBehavior="Forever"/>
                            <DoubleAnimation  Storyboard.TargetName="stop5" Storyboard.TargetProperty="Offset" To="2"
                                              From="1" RepeatBehavior="Forever"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>
            <Button.Background>
                <LinearGradientBrush>
                    <GradientStop Offset="-1" x:Name="stop1" Color="LightGray"/>
                    <GradientStop Offset="-0.5"  x:Name="stop2" Color="LightGray"/>
                    <GradientStop Offset="0" x:Name="stop3" Color="LightGray"/>
                    <GradientStop Offset="0.5" x:Name="stop4" Color="LightGray"/>
                    <GradientStop Offset="1"  x:Name="stop5" Color="LightGray"/>
                </LinearGradientBrush>
            </Button.Background>
        </Button>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900