Color Animation Problem

hi

I want to apply a color animation on my rectangle when I move my mouse over it. But I get a runtime exception when I attempt to do the same.

This is what my xaml code looks like-

<Rectangle Name="R1" Fill="Aqua" Width="100" Height="50" Stroke="Black" Margin="200">
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Rectangle.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="R1" Storyboard.TargetProperty="Fill" To="DarkBlue" Duration="0:0:0.25"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>

The exception is get is as follows-

The animation object 'System.Windows.Media.Animation' may not be used to animate the property 'Fill' because it is of an uncompatible type 'System.Windows.Media.Brush'

I understand where Im going wrong, but how else do I change the color of my rectangle need advice.

thanks

amit



Answer this question

Color Animation Problem

  • Anthony Whalley

    Here's the demo that appears in the docs.

    < xml version="1.0" encoding="utf-8" >
    <!-- SolidColorBrushExample.xaml
    This example shows how to animate the Opacity and Color
    properties of a SolidColorBrush.-->
    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    WindowTitle="SolidColorBrush Animation Example">
    <StackPanel Margin="20">

    <!-- The Opacity and Color of the SolidColorBrush
    used to fill this rectangle is animated. -->
    <Rectangle Width="100" Height="100">
    <Rectangle.Fill>
    <SolidColorBrush x:Name="MyAnimatedBrush" Color="Orange" />
    </Rectangle.Fill>
    <Rectangle.Triggers>

    <!-- Animates the brush's color to gray. -->
    <EventTrigger RoutedEvent="Rectangle.MouseEnter">
    <BeginStoryboard>
    <Storyboard>
    <ColorAnimation
    Storyboard.TargetName="MyAnimatedBrush"
    Storyboard.TargetProperty="(SolidColorBrush.Color)"
    To="Gray" Duration="0:0:1" />
    </Storyboard>
    </BeginStoryboard>
    </EventTrigger>

    <!-- Animates the brush's color to orange. -->
    <EventTrigger RoutedEvent="Rectangle.MouseLeave">
    <BeginStoryboard>
    <Storyboard>
    <ColorAnimation
    Storyboard.TargetName="MyAnimatedBrush"
    Storyboard.TargetProperty="(SolidColorBrush.Color)"
    To="Orange" Duration="0:0:1" />
    </Storyboard>
    </BeginStoryboard>
    </EventTrigger>

    <!-- Animates the brush's opacity. -->
    <EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown">
    <BeginStoryboard>
    <Storyboard>
    <DoubleAnimation
    Storyboard.TargetName="MyAnimatedBrush"
    Storyboard.TargetProperty="(SolidColorBrush.Opacity)"
    To="0.0" Duration="0:0:0.5" AutoReverse="True" />
    </Storyboard>
    </BeginStoryboard>
    </EventTrigger>
    </Rectangle.Triggers>
    </Rectangle>
    </StackPanel>
    </Page>


  • Gambrinus

    Amit Gandhi wrote:

    <Rectangle Name="R1" Fill="Aqua" Width="100" Height="50" Stroke="Black" Margin="200">
    <Rectangle.Triggers>
    <EventTrigger RoutedEvent="Rectangle.MouseEnter">
    <EventTrigger.Actions>
    <BeginStoryboard>
    <Storyboard>
    <ColorAnimation Storyboard.TargetName="R1" Storyboard.TargetProperty="Fill" To="DarkBlue" Duration="0:0:0.25"/>
    </Storyboard>
    </BeginStoryboard>
    </EventTrigger.Actions>
    </EventTrigger>
    </Rectangle.Triggers>
    </Rectangle>

    The exception is get is as follows-

    The animation object 'System.Windows.Media.Animation' may not be used to animate the property 'Fill' because it is of an uncompatible type 'System.Windows.Media.Brush'

    I am able to change the fill color of my ellipse if I write an event handler for MouseEnter event in the code behind file. Is there no way of achieving the same using ColorAnimation


  • Charlee Strong

    you have to use a brush for the fill and animate the brush color

  • Chrisk23

    thanks!
  • Color Animation Problem