#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.

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

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

About these ads

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

3 Responses to #508 – Displaying a Spinning Busy Indicator

  1. samuelhenry says:

    Hi Sean,

    I just started posting all the spinning busy indicators (radial throbbers) I encounter online. They’re everywhere. The Google+ Android app sometimes displays two at once.

    Long live the Throbber!

    Sam

  2. Michael DiSibio says:

    Sean…up all night trying to get DataTriggers to start and stop a simple progress bar. Either triggers were not firing, or using ‘Storyboard.TargetName’ was out of scope. Read every related post in StackOverflow and was still incredibly confused. Just hit your blog now. Everything working five minutes later. So, so, so, so much appreciated! – Mike D.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 381 other followers

%d bloggers like this: