如何在MouseEnter上暂停故事板并在MouseExit上恢复

时间:2014-07-14 16:14:02

标签: c# wpf animation

我有一个滚动的新闻自动收录器,通过故事板进行动画制作。它目前工作得很好,但我想暂停它,只要鼠标在TextBlock上,然后一旦鼠标移出TextBlock后恢复它

<WrapPanel x:Name="Ticker" >
<TextBlock Text="{Binding NewsTicker, IsAsync=True}" FontSize="18" Foreground="White"  > 
                    <TextBlock.RenderTransform>
                        <TranslateTransform x:Name="translate" />
                    </TextBlock.RenderTransform>
                    <TextBlock.Triggers> 
                        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                            <BeginStoryboard x:Name="NewsTicker">
                                <Storyboard RepeatBehavior="Forever">
                                    <DoubleAnimation From="{Binding ElementName=Ticker, Path=ActualWidth}" To="{Binding ElementName=Ticker, Path=ActualWidth, Converter={StaticResource NegConverter}}" Storyboard.TargetName="translate" Storyboard.TargetProperty="X" Duration="0:0:35" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="MouseEnter">
                            <EventTrigger.EnterActions>
                                <PauseStoryboard BeginStoryboardName="NewsTicker" />
                            </EventTrigger.EnterActions>
                            <EventTrigger.ExitActions>
                                <ResumeStoryboard BeginStoryboardName="NewsTicker" />
                            </EventTrigger.ExitActions>
                        </EventTrigger>
                    </TextBlock.Triggers>
                </TextBlock>
</WrapPanel>

我尝试了多种Pause和Resume Storyboard以及不同的RoutedEvents组合。我甚至删除了EventTrigger.ExitAction只是为了看看暂停是否有效,但它没有任何区别。

1 个答案:

答案 0 :(得分:1)

而不是使用EnterActions / ExitActions使用MouseEnterMouseLeave事件

<TextBlock.Triggers>
   <EventTrigger RoutedEvent="FrameworkElement.Loaded">
      <BeginStoryboard x:Name="NewsTicker">
         <Storyboard RepeatBehavior="Forever">
            <DoubleAnimation ... />
         </Storyboard>
      </BeginStoryboard>
   </EventTrigger>
   <EventTrigger RoutedEvent="MouseEnter">
      <PauseStoryboard BeginStoryboardName="NewsTicker" />
   </EventTrigger>
   <EventTrigger RoutedEvent="MouseLeave">
      <ResumeStoryboard BeginStoryboardName="NewsTicker" />
   </EventTrigger>
</TextBlock.Triggers>

由于事件没有状态,因此没有进入/退出操作,只有在事件发生时才会执行操作

EventTrigger Class

  

触发器对象具有Setters,EnterActions和ExitActions属性,这些属性根据特定属性的状态应用更改或操作,而 EventTrigger对象在指定的路由事件发生时启动一组操作