可见性上的WPF停止故事板已更改

时间:2010-01-05 23:15:00

标签: wpf xaml triggers visibility storyboard

我有一个带有故事板的UserControl,我想在控件的可见性发生变化时停止动画。

我创建了一个触发器来暂停动画并根据状态启动它,但我不断收到ArgumentException。

这是XAML:

<UserControl.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        <BeginStoryboard x:Name="ProgressAnimation_BeginStoryboard" Storyboard="{StaticResource ProgressAnimation}"/>
    </EventTrigger>
    <Trigger Property="Control.Visibility" Value="Collapsed">
        <PauseStoryboard BeginStoryboardName="ProgressAnimation_BeginStoryboard" />
    </Trigger>
    <Trigger Property="Control.Visibility" Value="Visible">
        <ResumeStoryboard BeginStoryboardName="ProgressAnimation_BeginStoryboard" />
    </Trigger>
</UserControl.Triggers>

,这是例外:

  

值“System.Windows.Media.Animation.PauseStoryboard”不是“System.Windows.SetterBase”类型,不能在此通用集合中使用。   参数名称:值

我如何在XAML中执行此操作?

谢谢, 劳尔

2 个答案:

答案 0 :(得分:17)

您可以使用控制模板:

<ControlTemplate>
    ... Control stuff here

    <ControlTemplate.Triggers>
        <Trigger Property="Visibility" Value="Visible">
            <Trigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource AnimationStoryboard}" x:Name="AnimationBeginStoryboard"/>
            </Trigger.EnterActions>
            <Trigger.ExitActions>
                <RemoveStoryboard BeginStoryboardName="AnimationBeginStoryboard"/>
            </Trigger.ExitActions>
        </Trigger>
    </ControlTemplate.Triggers>

</ControlTemplate>

答案 1 :(得分:-1)

看看这个sample from MSDN

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 <StackPanel>
  <Rectangle Name="TargetRect" Width="200" Height="200" Fill="Blue"/>
  <Button Name="Begin">BeginStoryboard</Button>
  <Button Name="Pause">PauseStoryboard</Button>
  <Button Name="Resume">ResumeStoryboard</Button>
  <StackPanel.Triggers>
   <EventTrigger SourceName="Begin" RoutedEvent="Button.Click">
    <BeginStoryboard Name="ColorStoryboard">
     <Storyboard TargetName="TargetRect">
      <ColorAnimation Storyboard.TargetProperty="Fill.Color" To="Red" Duration="0:0:3" RepeatBehavior="Forever" AutoReverse="True"/>
     </Storyboard>
    </BeginStoryboard>
   </EventTrigger>
   <EventTrigger SourceName="Pause" RoutedEvent="Button.Click">
    <PauseStoryboard BeginStoryboardName="ColorStoryboard"/>
   </EventTrigger>
   <EventTrigger SourceName="Resume" RoutedEvent="Button.Click">
    <ResumeStoryboard BeginStoryboardName="ColorStoryboard"/>
   </EventTrigger>
  </StackPanel.Triggers>
 </StackPanel>
</Page>

我能看到的唯一区别是他们在这里使用EventTrigger,但它的行为应该相同,至少根据我的经验。