#508 – Displaying a Spinning Busy Indicator

You’ll often see a circular spinning icon used in an application to indicate that some work is in progress. You can easily do this in WPF by animating an Image control.

In the example below, we have an Image control that animates (rotates) whenever a Busy property becomes true.  (The Button triggers this flag).  We start the rotation when Busy becomes true using a Storyboard and then remove the Storyboard when the flag goes back to being false.

        <Image Source="Images\Spinny3.jpg" Height="40" Width="40" Stretch="None"
               HorizontalAlignment="Center" VerticalAlignment="Center" Margin="15">
                <RotateTransform  CenterX="20" CenterY="20"/>
                        <DataTrigger Binding="{Binding Busy}" Value="True">
                                <BeginStoryboard Name="rotatingStoryboard">
                                            Storyboard.Target="{Binding TemplatedParent}"
                                            From="0" To="360" Duration="0:0:1" RepeatBehavior="Forever"/>
                                <RemoveStoryboard BeginStoryboardName="rotatingStoryboard"/>
        <Button Content="Start-Stop" HorizontalAlignment="Center" Margin="15" Padding="10,5" Click="Button_Click_1"/>

You could also set the Visibility of the Image based on the same flag (visible when busy).