---
name: creating-wpf-animations
description: "Creates WPF animations using Storyboard, Timeline, and EasingFunction patterns. Use when implementing UI transitions, state change visualizations, or interactive feedback effects."
---
# WPF Animation Patterns
WPF animations create visual effects by changing property values over time.
## 1. Animation Components
```
Storyboard (container)
├── Timeline (time control)
│ ├── Animation (value change)
│ │ ├── DoubleAnimation
│ │ ├── ColorAnimation
│ │ └── ...
│ └── AnimationUsingKeyFrames (keyframes)
│ ├── DoubleAnimationUsingKeyFrames
│ └── ...
└── EasingFunction (acceleration/deceleration)
```
---
## 2. Basic Animation (XAML)
### 2.1 DoubleAnimation
```xml
```
### 2.2 ColorAnimation
```xml
```
### 2.3 ThicknessAnimation (Margin, Padding)
```xml
```
---
## 3. EasingFunction
### 3.1 Main Easing Types
```xml
```
### 3.2 EasingMode
| Mode | Description |
|------|-------------|
| **EaseIn** | Slow at start, fast at end |
| **EaseOut** | Fast at start, slow at end |
| **EaseInOut** | Slow at both ends, fast in middle |
---
## 4. KeyFrame Animation
### 4.1 DoubleAnimationUsingKeyFrames
```xml
```
### 4.2 ObjectAnimationUsingKeyFrames (Discrete Values)
```xml
```
---
## 5. Transform Animation
### 5.1 RenderTransform Animation
```xml
```
### 5.2 Composite Transform Animation
```xml
```
---
## 6. Animation in Code
### 6.1 Basic Animation
```csharp
namespace MyApp.Animations;
using System;
using System.Windows;
using System.Windows.Media.Animation;
public static class AnimationHelper
{
///
/// Opacity fade animation
///
public static void FadeIn(UIElement element, double durationSeconds = 0.3)
{
var animation = new DoubleAnimation
{
From = 0,
To = 1,
Duration = TimeSpan.FromSeconds(durationSeconds),
EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut }
};
element.BeginAnimation(UIElement.OpacityProperty, animation);
}
public static void FadeOut(UIElement element, double durationSeconds = 0.3, Action? onCompleted = null)
{
var animation = new DoubleAnimation
{
From = 1,
To = 0,
Duration = TimeSpan.FromSeconds(durationSeconds),
EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseIn }
};
if (onCompleted is not null)
{
animation.Completed += (s, e) => onCompleted();
}
element.BeginAnimation(UIElement.OpacityProperty, animation);
}
}
```
### 6.2 Composite Animation with Storyboard
```csharp
namespace MyApp.Animations;
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
public static class StoryboardHelper
{
///
/// Scale up + fade in animation
///
public static void ScaleFadeIn(FrameworkElement element, double durationSeconds = 0.3)
{
// Setup transform
var scaleTransform = new ScaleTransform(0.8, 0.8);
element.RenderTransform = scaleTransform;
element.RenderTransformOrigin = new Point(0.5, 0.5);
element.Opacity = 0;
var storyboard = new Storyboard();
// Opacity animation
var opacityAnimation = new DoubleAnimation
{
To = 1,
Duration = TimeSpan.FromSeconds(durationSeconds),
EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut }
};
Storyboard.SetTarget(opacityAnimation, element);
Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath(UIElement.OpacityProperty));
storyboard.Children.Add(opacityAnimation);
// ScaleX animation
var scaleXAnimation = new DoubleAnimation
{
To = 1,
Duration = TimeSpan.FromSeconds(durationSeconds),
EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut }
};
Storyboard.SetTarget(scaleXAnimation, element);
Storyboard.SetTargetProperty(scaleXAnimation,
new PropertyPath("RenderTransform.ScaleX"));
storyboard.Children.Add(scaleXAnimation);
// ScaleY animation
var scaleYAnimation = new DoubleAnimation
{
To = 1,
Duration = TimeSpan.FromSeconds(durationSeconds),
EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut }
};
Storyboard.SetTarget(scaleYAnimation, element);
Storyboard.SetTargetProperty(scaleYAnimation,
new PropertyPath("RenderTransform.ScaleY"));
storyboard.Children.Add(scaleYAnimation);
storyboard.Begin();
}
}
```
### 6.3 Stop/Resume Animation
```csharp
namespace MyApp.Animations;
using System.Windows;
using System.Windows.Media.Animation;
public sealed class AnimationController
{
private readonly Storyboard _storyboard;
private readonly FrameworkElement _target;
public AnimationController(Storyboard storyboard, FrameworkElement target)
{
_storyboard = storyboard;
_target = target;
}
public void Start()
{
_storyboard.Begin(_target, isControllable: true);
}
public void Pause()
{
_storyboard.Pause(_target);
}
public void Resume()
{
_storyboard.Resume(_target);
}
public void Stop()
{
_storyboard.Stop(_target);
}
public void Seek(TimeSpan offset)
{
_storyboard.Seek(_target, offset, TimeSeekOrigin.BeginTime);
}
}
```
---
## 7. VisualStateManager Integration
### 7.1 State-Based Animation
```xml
```
---
## 8. Performance Considerations
| Property | Performance | Description |
|----------|-------------|-------------|
| **Opacity** | ⭐⭐⭐ | Most efficient |
| **RenderTransform** | ⭐⭐⭐ | No layout recalculation |
| **Clip** | ⭐⭐ | Medium performance |
| **Width/Height** | ⭐ | Causes layout recalculation |
| **Margin** | ⭐ | Causes layout recalculation |
```csharp
// Performance optimization hints
RenderOptions.SetBitmapScalingMode(element, BitmapScalingMode.LowQuality);
Timeline.SetDesiredFrameRate(storyboard, 30); // Default 60fps → 30fps
```
---
## 9. References
- [Animation Overview - Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/animation-overview)
- [Storyboards Overview - Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/storyboards-overview)