【问题标题】:Trigger Property to restart storyboard to first state触发属性以将情节提要重新启动到第一个状态
【发布时间】:2014-03-12 01:33:20
【问题描述】:

我有 2 个触发事件,都带有 <Trigger.EnterActions><Trigger.ExitActions>,它们是 IsPressedIsMouseOver

当我点击一个按钮时,它将播放 <Trigger Property="IsPressed" Value="True"> 故事板:

<Trigger Property="IsPressed" Value="True">
   <Trigger.EnterActions>
       <BeginStoryboard Storyboard="{StaticResource MouseDownTimeLine}"/>
   </Trigger.EnterActions>
   <Trigger.ExitActions>
        <BeginStoryboard Storyboard="{StaticResource MouseUpTimeLine}"/>
   </Trigger.ExitActions>
 </Trigger>

这是链接到这个故事板:

<Storyboard x:Key="MouseDownTimeLine" >
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Pressed" Storyboard.TargetProperty="Opacity" >
        <SplineDoubleKeyFrame KeyTime="00:00:00.25" Value="1" />
     </DoubleAnimationUsingKeyFrames>
</Storyboard>

<Storyboard x:Key="MouseUpTimeLine">
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Pressed" Storyboard.TargetProperty="Opacity" >
        <SplineDoubleKeyFrame KeyTime="00:00:00.25" Value="1"/>
     </DoubleAnimationUsingKeyFrames>
</Storyboard>

不同的按钮也链接到同一个故事板。单击第二个按钮时,我希望它按原样播放情节提要,然后让第一个按钮返回正常状态。这是故事板的这一部分。

<Storyboard x:Key="MouseExitTimeLine">
     <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Normal" Storyboard.TargetProperty="Opacity" >
          <SplineDoubleKeyFrame KeyTime="00:00:00.00" Value="1"/>
     </DoubleAnimationUsingKeyFrames>

      <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Hover" Storyboard.TargetProperty="Opacity" >
           <SplineDoubleKeyFrame KeyTime="00:00:00.00" Value="0"/>
       </DoubleAnimationUsingKeyFrames>
</Storyboard>

当第二个按钮被点击时如何触发第一个按钮重置为正常

【问题讨论】:

    标签: wpf xaml triggers storyboard wpf-animation


    【解决方案1】:

    也许我在这里错了,但您似乎在按钮模板中加入了单选按钮功能。当您单击一个按钮时,它会保持“已单击”(或选中)状态,而当您单击另一个按钮时,它会“释放”第一个按钮的单击。

    我会将模板的目标类型更改为RadioButton,并将触发器更改为:

    <Trigger Property="IsChecked" Value="True">
       <Trigger.EnterActions>
           <BeginStoryboard x:Name="ButtonCheckedStoryboardBegin"
                            Storyboard="{StaticResource MouseDownTimeLine}"/>
       </Trigger.EnterActions>
       <Trigger.ExitActions>
           <StopStoryboard BeginStoryboardName="ButtonCheckedStoryboardBegin"/>
       </Trigger.ExitActions>
    </Trigger>
    

    使用 StopStoryboard 因为 MouseExitTimeLine 中的持续时间为零。如果您确实需要退出动画的持续时间,请使用:

    <Trigger Property="IsChecked" Value="True">
       <Trigger.EnterActions>
           <BeginStoryboard Storyboard="{StaticResource MouseDownTimeLine}"/>
       </Trigger.EnterActions>
       <Trigger.ExitActions>
           <BeginStoryboard Storyboard="{StaticResource MouseExitTimeLine}"/>
       </Trigger.ExitActions>
    </Trigger>
    

    [第二个选项可以按原样使用 - 持续时间为零,但不太优雅,IMO]

    此外,MouseExitTimeLine 中“Hover”的动画是多余的,可以删除,因为 MouseDownTimeLine 没有在“Hover”上设置任何内容。

    现在,在布局中RadioButton 的每个实例上,添加GroupName="&lt;Some string here&gt;"。在任何给定时间点,只能选中一个组中的一个单选按钮。

    【讨论】:

    • 当我将模板更改为 RadioButton 时,出现大量错误。 RadioButton ControlTemplate TargetType does not match templated type Button ?
    • 您需要将Button的实例更改为RadioButton
    猜你喜欢
    • 2011-07-11
    • 2019-04-25
    • 1970-01-01
    • 2017-05-27
    • 2012-11-05
    • 2017-08-19
    • 2012-04-27
    • 2015-03-06
    • 2011-01-12
    相关资源
    最近更新 更多