WPF XAML Animation How do i stop it when bound boo

2019-07-13 06:17发布

I want the animation to stop when the boolean CanAnimate becomes false. It starts on true, so how do i tell it to stop when CanAnimate is false? (The CanAnimate bool is set inside a SelectedItem setter)

<Border BorderBrush="Black" BorderThickness="2" Margin="1" Name="ReviewNote">
    <Border.Style>
        <Style TargetType="Border">
           <Style.Triggers>
               <DataTrigger Binding="{Binding CanAnimate}" Value="True">
                   <DataTrigger.EnterActions>
                       <BeginStoryboard>
                           <Storyboard>
                              <DoubleAnimation 
                                    Storyboard.TargetProperty="(Border.Opacity)"
                                    From="1.0" To="0.0" AutoReverse="True" 
                                    RepeatBehavior="Forever" />
                            </Storyboard>
                       </BeginStoryboard>
                   </DataTrigger.EnterActions>
               </DataTrigger>
            </Style.Triggers>
         </Style>
    </Border.Style>
   <TextBlock/>
</Border>

1条回答
来,给爷笑一个
2楼-- · 2019-07-13 06:46

You can try using the ExitActions on the DataTrigger to stop the animation, by overriding with another animation. For instance:

<DataTrigger Binding="{Binding CanAnimate}" Value="True">
   <DataTrigger.EnterActions>
       <BeginStoryboard>
           <Storyboard>
              <DoubleAnimation 
                    Storyboard.TargetProperty="(Border.Opacity)"
                    From="1.0" To="0.0" AutoReverse="True" 
                    RepeatBehavior="Forever" />
            </Storyboard>
       </BeginStoryboard>
   </DataTrigger.EnterActions>
   <DataTrigger.ExitActions>
        <BeginStoryboard>
           <Storyboard>
              <DoubleAnimation 
                    Storyboard.TargetProperty="(Border.Opacity)"
                    From="0.0" To="0.0" Duration="0:0:0.0" FillBehavior="HoldEnd" />
            </Storyboard>
       </BeginStoryboard>
   </DataTrigger.ExitActions>
</DataTrigger>

Alternatively, there is a way to stop storyboards in XAML by name, again you could use ExitActions for this. This previous question shows the way.

Hope this helps!

查看更多
登录 后发表回答