在WPF中按下IsPressed时启动动画

时间:2014-10-24 16:20:19

标签: c# wpf xaml

我的XAML中有DoubleAnimation,根据按下的按钮向上和向下滚动ScrollViewer。当按下按钮时,我试图无限期地将其切换为动画(滚动),并在释放按钮时停止动画。我似乎无法弄清楚如何在XAML中做到这一点。我希望避免在代码中执行此操作,因为我使用的是MVVM模式,并且非常善于将UI与逻辑分离。这是我的动画代码

<UserControl.Triggers>
    <EventTrigger RoutedEvent="Button.Click" SourceName="ScrollUp">
        <BeginStoryboard HandoffBehavior="Compose">
            <Storyboard Duration="0:0:1">
                <DoubleAnimation 
                    Storyboard.TargetName="ScrollViewerView"
                    Storyboard.TargetProperty="(wpf:ScrollViewerBinding.VerticalOffset)" 
                    To="{Binding NewVerticalScrollPositionUp}">
                    <DoubleAnimation.EasingFunction>
                        <SineEase EasingMode="EaseInOut"/>
                    </DoubleAnimation.EasingFunction>
                </DoubleAnimation>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
    <EventTrigger RoutedEvent="Button.Click" SourceName="ScrollDown">
        <BeginStoryboard HandoffBehavior="Compose">
            <Storyboard Duration="0:0:0:1">
                <DoubleAnimation 
                    Storyboard.TargetName="ScrollViewerView"
                    Storyboard.TargetProperty="(wpf:ScrollViewerBinding.VerticalOffset)" 
                    To="{Binding NewVerticalScrollPositionDown}">
                    <DoubleAnimation.EasingFunction>
                        <QuadraticEase EasingMode="EaseInOut"/>
                    </DoubleAnimation.EasingFunction>
                </DoubleAnimation>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</UserControl.Triggers>

当我按下向下按钮时,它会动画,向下滚动ScrollViewer。当我按下向上按钮时也是如此。但我无法找到一种让按钮停止时动画继续运行的方法。

2 个答案:

答案 0 :(得分:1)

使用RepeatButton代替常规ButtonRepeatButton的功能类似于ScrollBar上的箭头按钮,并且只要按下它就会以固定的间隔继续发射。请注意,其Interval属性以毫秒为单位进行测量。

答案 1 :(得分:0)

创建一个连续运行并在按下按钮时启动的新动画。然后在按钮释放上停止连续动画。