#509 – Displaying a Busy Indicator in a Data Template

Here’s another variation on the earlier example of displaying a spinning busy indicator using an Image control.  This example shows how to do the same thing when the Image control is hosted in a DataTemplate.

In the code below, the Image control is part of the data template.  If the Movie object’s Loading flag is true, the image will be displayed and the Storyboard will animate (spin) it.  Once Loading becomes false, the Image stops animating and disappears.

<Window x:Class="WpfApplication11.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
        Title="Spinny" Height="500" Width="300">

    <Window.Resources>
        <ResourceDictionary>
            <controls:BooleanToVisibilityConverter x:Key="boolToVisibilityConverter" />
        </ResourceDictionary>
    </Window.Resources>

    <Grid Margin="15">
        <ListBox ItemsSource="{Binding MovieList}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Name="imgBusy" Source="Images\spinny3.jpg" Height="40" Width="40"
                               Visibility="{Binding Path=Loading, Converter={StaticResource boolToVisibilityConverter}}">
                            <Image.RenderTransform>
                                <RotateTransform Angle="0" CenterX="20" CenterY="20"/>
                            </Image.RenderTransform>
                        </Image>

                        <Label Content="{Binding Title}"/>
                        <Label Content="{Binding Year}"/>
                        <Label Content="{Binding Director}"/>
                    </StackPanel>

                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding ElementName=imgBusy, Path=Visibility}" Value="Visible">
                            <DataTrigger.EnterActions>
                                <BeginStoryboard Name="rotatingStoryboard">
                                    <Storyboard>
                                        <DoubleAnimation
                                            Storyboard.TargetName="imgBusy"
                                            Storyboard.TargetProperty="RenderTransform.(RotateTransform.Angle)"
                                            From="0" To="360" Duration="0:0:1.5" RepeatBehavior="Forever"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>

                            <!-- Must remove the Storyboard, or it will continue animating forever in the background -->
                            <DataTrigger.ExitActions>
                                <RemoveStoryboard BeginStoryboardName="rotatingStoryboard"/>
                            </DataTrigger.ExitActions>
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

Advertisement

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