Search code examples
wpfanimationdatatrigger

How to trigger storyboard every time


I have a storyboard that sets an image to visible, and then fades the image in and out over the course of a couple of a seconds. The storyboard triggers when the property IsSaveDiskVisible gets set to true. IsSaveDiskVisible gets set to true when various textboxes lose focus.

The storyboard works on the first time that one of the textboxes loses focus and sets IsSaveDiskVisible to true. However, subsequent tries of the textboxes losing focus do not trigger the storyboard because IsSaveDiskVisible is already set to true.

How do I get the storyboard to trigger with each time that any of the textboxes lose focus?

<Image x:Name="imgDiskBlack" Source="{Binding SaveDiskImg}" Stretch="None" Margin="4,0,0,0" Visibility="Collapsed">
    <Image.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSaveDiskVisible}" Value="true">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
                                    <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}" />
                                    <DiscreteObjectKeyFrame KeyTime="0:0:2.5" Value="{x:Static Visibility.Collapsed}" />
                                </ObjectAnimationUsingKeyFrames>
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
                                    <SplineDoubleKeyFrame KeyTime="0:0:1" Value="0.0" />
                                    <SplineDoubleKeyFrame KeyTime="0:0:2" Value="1.0" />
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

Solution

  • If it's bound to IsSaveDiskVisible, can't you set IsSaveDiskVisible = false whenever the user changes any properties? I'm assuming you are trying to fade a disk image to show that the file is saving or something like that. If so, you could either reset the property after someone makes a change (so that they can click a button and save changes), or reset it at the end of the save method.