开发者

WPF Stop Storyboard on Visibility Changed

开发者 https://www.devze.com 2022-12-15 18:24 出处:网络
I have a UserControl with a story board and I want to stop the animation when the control\'s Visibility changes.

I have a UserControl with a story board and I want to stop the animation when the control's Visibility changes.

I created a Trigger to pause the animation and start it depending on the state, but I keep getting an ArgumentException.

Here is the XAML:

<UserControl.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        <BeginStoryboard x:Name="ProgressAnimation_BeginStoryboard" Storyboard="{StaticResource ProgressAnimation}"/>
    </EventTrig开发者_如何学Pythonger>
    <Trigger Property="Control.Visibility" Value="Collapsed">
        <PauseStoryboard BeginStoryboardName="ProgressAnimation_BeginStoryboard" />
    </Trigger>
    <Trigger Property="Control.Visibility" Value="Visible">
        <ResumeStoryboard BeginStoryboardName="ProgressAnimation_BeginStoryboard" />
    </Trigger>
</UserControl.Triggers>

and here is the Exception:

The value "System.Windows.Media.Animation.PauseStoryboard" is not of type "System.Windows.SetterBase" and cannot be used in this generic collection. Parameter name: value

How would I do this in XAML ?

Thanks, Raul


You may do it using a control template:

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


Take a look at this 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>

The only difference I can see is that they are using EventTrigger here, but it should behave the same, at least in my experience.

0

精彩评论

暂无评论...
验证码 换一张
取 消