Search code examples
wpfwpf-controlsbackgroundanimated

Animated background control in WPF?


In my WPF application, I want to have an ambient animated background similar to Media Center's background. Is there a free control that offers this?


Solution

  • I would prefer to animate the background of a border via storyboard. It's pretty easy and you can build a animation as complex as you like. Here is a short example:

    <Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Window.Triggers>
            <EventTrigger RoutedEvent="Window.Loaded">
                <BeginStoryboard>
                    <Storyboard AutoReverse="True" BeginTime="0" >
                        <DoubleAnimation Storyboard.TargetName="Foo"
                                         Storyboard.TargetProperty="Offset"
                                         From="0.2" To="0.8" Duration="0:0:10"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Window.Triggers>
        <Border>
            <Border.Background>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                        <GradientStop Color="Yellow" Offset="0"/>
                        <GradientStop Color="Orange" Offset="0.2"  x:Name="Foo"/>
                        <GradientStop Color="Red" Offset="1"/>               
                </LinearGradientBrush>
            </Border.Background>
        <!-- put your windowcontent(grid etc.) here -->
        </Border>
    </Window>
    

    You should also see the MSDN article Animation Overview.