Let's create a basic animation window on a data trigger, where the size of the button changes on ButtonClick event and the background color of a button and panel changes for 8 seconds.
Also, let's add a heart shape path, which will be triggered with a boolean to visibility converter.
Let's build this:
Tip
We are following MVVM.
Button
Background, Height & Width are managed by animation.
- <Button x:Name="ClickMe"
- Content="Start The Animation"
- Height="20"
- Width="120"
- VerticalAlignment="Center"
- HorizontalAlignment="Center"
- Background="Transparent"
- BorderBrush="salmon"
- Margin="-10 110 0 0"
- Command="{Binding AminationButtonClicked}">
- <Button.Style>
- <Style TargetType="Button" >
- <Style.Triggers>
- <DataTrigger Binding="{Binding IsButtonClicked}" Value="True">
- <DataTrigger.EnterActions>
- <BeginStoryboard>
- <Storyboard Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" Duration="0:0:8">
- <ColorAnimation To="Gray" Duration="0:0:0"/>
- <ColorAnimation To="Coral" Duration="0:0:1"/>
- <ColorAnimation To="LightBlue" Duration="0:0:2"/>
- <ColorAnimation To="Salmon" Duration="0:0:3"/>
- <ColorAnimation To="White" Duration="0:0:4"/>
- <ColorAnimation To="Salmon" Duration="0:0:5"/>
- <ColorAnimation To="LightBlue" Duration="0:0:6"/>
- <ColorAnimation To="Coral" Duration="0:0:7"/>
- <ColorAnimation To="Gray" Duration="0:0:8"/>
- </Storyboard>
- </BeginStoryboard>
- <BeginStoryboard>
- <Storyboard Storyboard.TargetProperty="Width" Duration="0:0:8">
- <DoubleAnimationUsingKeyFrames>
- <LinearDoubleKeyFrame Value="150" KeyTime="0:0:0"/>
- <LinearDoubleKeyFrame Value="200" KeyTime="0:0:1"/>
- <LinearDoubleKeyFrame Value="250" KeyTime="0:0:2"/>
- <LinearDoubleKeyFrame Value="300" KeyTime="0:0:3"/>
- <LinearDoubleKeyFrame Value="350" KeyTime="0:0:4"/>
- <LinearDoubleKeyFrame Value="300" KeyTime="0:0:5"/>
- <LinearDoubleKeyFrame Value="250" KeyTime="0:0:6"/>
- <LinearDoubleKeyFrame Value="200" KeyTime="0:0:7"/>
- <LinearDoubleKeyFrame Value="150" KeyTime="0:0:8"/>
- </DoubleAnimationUsingKeyFrames>
- </Storyboard>
- </BeginStoryboard>
- <BeginStoryboard>
- <Storyboard Storyboard.TargetProperty="Height" Duration="0:0:8">
- <DoubleAnimationUsingKeyFrames>
- <LinearDoubleKeyFrame Value="20" KeyTime="0:0:0"/>
- <LinearDoubleKeyFrame Value="100" KeyTime="0:0:1"/>
- <LinearDoubleKeyFrame Value="250" KeyTime="0:0:2"/>
- <LinearDoubleKeyFrame Value="300" KeyTime="0:0:3"/>
- <LinearDoubleKeyFrame Value="350" KeyTime="0:0:4"/>
- <LinearDoubleKeyFrame Value="300" KeyTime="0:0:5"/>
- <LinearDoubleKeyFrame Value="250" KeyTime="0:0:6"/>
- <LinearDoubleKeyFrame Value="100" KeyTime="0:0:7"/>
- <LinearDoubleKeyFrame Value="20" KeyTime="0:0:8"/>
- </DoubleAnimationUsingKeyFrames>
- </Storyboard>
- </BeginStoryboard>
- </DataTrigger.EnterActions>
- </DataTrigger>
- </Style.Triggers>
- </Style>
- </Button.Style>
- </Button>
Let's put this button inside a grid. Notice grid has animation for background color.
- <Window x:Class="A.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- Title="MainWindow" Height="500" Width="500">
- <Window.Resources>
- <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
- </Window.Resources>
- <Grid>
- <Button x:Name="ClickMe"
- Content="Start The Animation"
- Height="20"
- Width="120"
- VerticalAlignment="Center"
- HorizontalAlignment="Center"
- Background="Transparent"
- BorderBrush="salmon"
- Margin="-10 110 0 0"
- Command="{Binding AminationButtonClicked}">
- <!-- Above code here -->
- </Button>
- <Grid.Style>
- <Style TargetType="Grid">
- <Setter Property="Background" Value="White"/>
- <Style.Triggers>
- <DataTrigger Binding="{Binding IsButtonClicked}" Value="True">
- <DataTrigger.EnterActions>
- <BeginStoryboard>
- <Storyboard Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" Duration="0:0:8">
- <ColorAnimation To="#00897b" Duration="0:0:0"/>
- <ColorAnimation To="#363636" Duration="0:0:1"/>
- <ColorAnimation To="#969696" Duration="0:0:2"/>
- <ColorAnimation To="mistyrose" Duration="0:0:3"/>
- <ColorAnimation To="White" Duration="0:0:4"/>
- <ColorAnimation To="lightpink" Duration="0:0:5"/>
- <ColorAnimation To="#969696" Duration="0:0:6"/>
- <ColorAnimation To="#363636" Duration="0:0:7"/>
- <ColorAnimation To="#00897b" Duration="0:0:8"/>
- </Storyboard>
- </BeginStoryboard>
- </DataTrigger.EnterActions>
- </DataTrigger>
- </Style.Triggers>
- </Style>
- </Grid.Style>
- </Grid>
- </Window>
Let's add that heart shape.
- <Path Stroke="Red" StrokeThickness="3"
- Data="M 241,200
- A 20,20 0 0 0 200,240
- C 210,250 240,270 240,270
- C 240,270 260,260 280,240
- A 20,20 0 0 0 239,200"
- Visibility="{Binding IsButtonClicked, Converter={StaticResource BooleanToVisibilityConverter}}">
- <Path.Style>
- <Style TargetType="Path">
- <Setter Property="Fill" Value="Black"/>
- <Style.Triggers>
- <DataTrigger Binding="{Binding IsButtonClicked}" Value="True">
- <DataTrigger.EnterActions>
- <BeginStoryboard>
- <Storyboard Storyboard.TargetProperty="Fill.(SolidColorBrush.Color)" Duration="0:0:8">
- <ColorAnimation To="Red" Duration="0:0:0"/>
- <ColorAnimation To="#161d20" Duration="0:0:1"/>
- <ColorAnimation To="#36498f" Duration="0:0:2"/>
- <ColorAnimation To="LightPink" Duration="0:0:3"/>
- <ColorAnimation To="Pink" Duration="0:0:4"/>
- <ColorAnimation To="LightPink" Duration="0:0:5"/>
- <ColorAnimation To="#36498f" Duration="0:0:6"/>
- <ColorAnimation To="#161d20" Duration="0:0:7"/>
- <ColorAnimation To="Red" Duration="0:0:8"/>
- </Storyboard>
- </BeginStoryboard>
- </DataTrigger.EnterActions>
- </DataTrigger>
- </Style.Triggers>
- </Style>
- </Path.Style>
- </Path>
Let's see our entire MainWindow.xaml
- <Window x:Class="WPFAPP.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- Title="MainWindow" Height="500" Width="500">
- <Window.Resources>
- <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
- </Window.Resources>
- <Grid>
- <Button x:Name="ClickMe"
- Content="Start The Animation"
- Height="20"
- Width="120"
- VerticalAlignment="Center"
- HorizontalAlignment="Center"
- Background="Transparent"
- BorderBrush="salmon"
- Margin="-10 110 0 0"
- Command="{Binding AminationButtonClicked}">
- <Button.Style>
- <Style TargetType="Button" >
- <Style.Triggers>
- <DataTrigger Binding="{Binding IsButtonClicked}" Value="True">
- <DataTrigger.EnterActions>
- <BeginStoryboard>
- <Storyboard Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" Duration="0:0:8">
- <ColorAnimation To="Gray" Duration="0:0:0"/>
- <ColorAnimation To="Coral" Duration="0:0:1"/>
- <ColorAnimation To="LightBlue" Duration="0:0:2"/>
- <ColorAnimation To="Salmon" Duration="0:0:3"/>
- <ColorAnimation To="White" Duration="0:0:4"/>
- <ColorAnimation To="Salmon" Duration="0:0:5"/>
- <ColorAnimation To="LightBlue" Duration="0:0:6"/>
- <ColorAnimation To="Coral" Duration="0:0:7"/>
- <ColorAnimation To="Gray" Duration="0:0:8"/>
- </Storyboard>
- </BeginStoryboard>
- <BeginStoryboard>
- <Storyboard Storyboard.TargetProperty="Width" Duration="0:0:8">
- <DoubleAnimationUsingKeyFrames>
- <LinearDoubleKeyFrame Value="150" KeyTime="0:0:0"/>
- <LinearDoubleKeyFrame Value="200" KeyTime="0:0:1"/>
- <LinearDoubleKeyFrame Value="250" KeyTime="0:0:2"/>
- <LinearDoubleKeyFrame Value="300" KeyTime="0:0:3"/>
- <LinearDoubleKeyFrame Value="350" KeyTime="0:0:4"/>
- <LinearDoubleKeyFrame Value="300" KeyTime="0:0:5"/>
- <LinearDoubleKeyFrame Value="250" KeyTime="0:0:6"/>
- <LinearDoubleKeyFrame Value="200" KeyTime="0:0:7"/>
- <LinearDoubleKeyFrame Value="150" KeyTime="0:0:8"/>
- </DoubleAnimationUsingKeyFrames>
- </Storyboard>
- </BeginStoryboard>
- <BeginStoryboard>
- <Storyboard Storyboard.TargetProperty="Height" Duration="0:0:8">
- <DoubleAnimationUsingKeyFrames>
- <LinearDoubleKeyFrame Value="20" KeyTime="0:0:0"/>
- <LinearDoubleKeyFrame Value="100" KeyTime="0:0:1"/>
- <LinearDoubleKeyFrame Value="250" KeyTime="0:0:2"/>
- <LinearDoubleKeyFrame Value="300" KeyTime="0:0:3"/>
- <LinearDoubleKeyFrame Value="350" KeyTime="0:0:4"/>
- <LinearDoubleKeyFrame Value="300" KeyTime="0:0:5"/>
- <LinearDoubleKeyFrame Value="250" KeyTime="0:0:6"/>
- <LinearDoubleKeyFrame Value="100" KeyTime="0:0:7"/>
- <LinearDoubleKeyFrame Value="20" KeyTime="0:0:8"/>
- </DoubleAnimationUsingKeyFrames>
- </Storyboard>
- </BeginStoryboard>
- </DataTrigger.EnterActions>
- </DataTrigger>
- </Style.Triggers>
- </Style>
- </Button.Style>
- </Button>
- <Path Stroke="Red" StrokeThickness="3"
- Data="M 241,200
- A 20,20 0 0 0 200,240
- C 210,250 240,270 240,270
- C 240,270 260,260 280,240
- A 20,20 0 0 0 239,200"
- Visibility="{Binding IsButtonClicked, Converter={StaticResource BooleanToVisibilityConverter}}">
- <Path.Style>
- <Style TargetType="Path">
- <Setter Property="Fill" Value="Black"/>
- <Style.Triggers>
- <DataTrigger Binding="{Binding IsButtonClicked}" Value="True">
- <DataTrigger.EnterActions>
- <BeginStoryboard>
- <Storyboard Storyboard.TargetProperty="Fill.(SolidColorBrush.Color)" Duration="0:0:8">
- <ColorAnimation To="Red" Duration="0:0:0"/>
- <ColorAnimation To="#161d20" Duration="0:0:1"/>
- <ColorAnimation To="#36498f" Duration="0:0:2"/>
- <ColorAnimation To="LightPink" Duration="0:0:3"/>
- <ColorAnimation To="Pink" Duration="0:0:4"/>
- <ColorAnimation To="LightPink" Duration="0:0:5"/>
- <ColorAnimation To="#36498f" Duration="0:0:6"/>
- <ColorAnimation To="#161d20" Duration="0:0:7"/>
- <ColorAnimation To="Red" Duration="0:0:8"/>
- </Storyboard>
- </BeginStoryboard>
- </DataTrigger.EnterActions>
- </DataTrigger>
- </Style.Triggers>
- </Style>
- </Path.Style>
- </Path>
- <Grid.Style>
- <Style TargetType="Grid">
- <Setter Property="Background" Value="White"/>
- <Style.Triggers>
- <DataTrigger Binding="{Binding IsButtonClicked}" Value="True">
- <DataTrigger.EnterActions>
- <BeginStoryboard>
- <Storyboard Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" Duration="0:0:8">
- <ColorAnimation To="#00897b" Duration="0:0:0"/>
- <ColorAnimation To="#363636" Duration="0:0:1"/>
- <ColorAnimation To="#969696" Duration="0:0:2"/>
- <ColorAnimation To="mistyrose" Duration="0:0:3"/>
- <ColorAnimation To="White" Duration="0:0:4"/>
- <ColorAnimation To="lightpink" Duration="0:0:5"/>
- <ColorAnimation To="#969696" Duration="0:0:6"/>
- <ColorAnimation To="#363636" Duration="0:0:7"/>
- <ColorAnimation To="#00897b" Duration="0:0:8"/>
- </Storyboard>
- </BeginStoryboard>
- </DataTrigger.EnterActions>
- </DataTrigger>
- </Style.Triggers>
- </Style>
- </Grid.Style>
- </Grid>
- </Window>
We need to bind command AminationButtonClicked and bool property to IsButtonClicked.
Let's see how MainWindowViewModel would look:
- using A.Entities;
- using Prism.Commands;
- using Prism.Mvvm;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
-
- namespace WPFAPP
- {
- class MainWindowViewModel : BindableBase
- {
- #region Properties
- private bool _isButtonClicked;
-
- public bool IsButtonClicked
- {
- get { return _isButtonClicked; }
- set { SetProperty(ref _isButtonClicked , value); }
- }
-
- public DelegateCommand<object> AminationButtonClicked { get; set; }
- #endregion
-
- #region Constructor
- public MainWindowViewModel()
- {
- AminationButtonClicked = new DelegateCommand<object>(SetButtonClicked);
- }
- #endregion
-
- private void SetButtonClicked(object obj)
- {
- IsButtonClicked = true;
- }
- }
- }
Perfect, you are all set.
Now, this is just to give you an idea of how you can develop basic animation in WPF.
You can, of course, do a lot with animation in WPF.
Take full advantage of this feature. Feel free to connect @
Thank you. And as always, happy coding!