#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"
        Title="Spinny" Height="500" Width="300">

            <controls:BooleanToVisibilityConverter x:Key="boolToVisibilityConverter" />

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

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

                        <DataTrigger Binding="{Binding ElementName=imgBusy, Path=Visibility}" Value="Visible">
                                <BeginStoryboard Name="rotatingStoryboard">
                                            From="0" To="360" Duration="0:0:1.5" RepeatBehavior="Forever"/>

                            <!-- Must remove the Storyboard, or it will continue animating forever in the background -->
                                <RemoveStoryboard BeginStoryboardName="rotatingStoryboard"/>


About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: