wpfwpf-animation

Border background color continous animation


I want that my border is continuously changing color from yellow to red. I tried this code, but it doesn't work. Why?

<Border Background="Yellow">
    <Border.Triggers>
        <EventTrigger RoutedEvent="Border.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" From="Yellow" To="Red" Duration="0:0:0.2"
                                            RepeatBehavior="Forever"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Border.Triggers>
</Border>

Solution

  • The Yellow SolidColorBrush that is assigned to the Background may not be animatable.

    It is safer to declare it like this:

    <Border>
        <Border.Background>
            <SolidColorBrush Color="Yellow"/>
        </Border.Background>
        <Border.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation
                            Storyboard.TargetProperty="Background.Color"
                            From="Yellow" To="Red" Duration="0:0:0.2"
                            AutoReverse="True" RepeatBehavior="Forever"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Border.Triggers>
    </Border>