WPF Graphics: Animated Elements Sample

This sample demonstrates how to create and control animated elements within a Windows Presentation Foundation (WPF) application. Animation in WPF can be achieved using the built-in animation classes or by leveraging CSS for web-based representations.

Basic Animated Element

Here is a simple animated rectangle that pulses in size and opacity.

XAML Equivalent (Conceptual)

In WPF, you would typically define animations in XAML:

<Rectangle Width="100" Height="100" Fill="#0078D4" RadiusX="10" RadiusY="10">
    <Rectangle.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard RepeatBehavior="Forever" AutoReverse="True">
                    <DoubleAnimationUsingKeyFrames>
                        <EasingDoubleKeyFrame KeyTime="0%" Value="1.0"/>
                        <EasingDoubleKeyFrame KeyTime="100%" Value="1.1"/>
                    </DoubleAnimationUsingKeyFrames>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
                        <EasingDoubleKeyFrame KeyTime="0%" Value="1.0"/>
                        <EasingDoubleKeyFrame KeyTime="100%" Value="0.8"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Rectangle.Triggers>
</Rectangle>

Animated Text

This section shows text that appears to flash or fade in and out.

WPF Animations!

XAML Equivalent (Conceptual)

<TextBlock Text="WPF Animations!" FontSize="32" FontWeight="Bold" Foreground="#D13434">
    <TextBlock.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard RepeatBehavior="Forever">
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
                        <LinearDoubleKeyFrame KeyTime="0%" Value="1.0"/>
                        <LinearDoubleKeyFrame KeyTime="50%" Value="0.2"/>
                        <LinearDoubleKeyFrame KeyTime="100%" Value="1.0"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </TextBlock.Triggers>
</TextBlock>

Controlling Animations

In a real WPF application, you can control animations programmatically using C# or VB.NET. For web simulation, we can use JavaScript to toggle styles or trigger animations.