2010-01-05 77 views
8

我有一個用戶控制故事板,並且我想在控件的可見性更改時停止動畫。WPF停止可見性更改的故事板

我創建了一個觸發器來暫停動畫,並根據狀態啓動它,但我一直得到一個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中執行此操作?

感謝, 勞爾

回答

0

看看這個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在這裏,但它應該具有相同的行爲,至少在我的經驗。

+0

這不會起作用,因爲用戶控件只能包含EventTriggers,而我需要一個。 – HaxElit 2010-01-05 23:39:18

14

您可以使用控制模板,它做的事:

<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>