WPF动画仅在一个方向上移动

时间:2013-12-03 14:46:11

标签: wpf wpf-animation

我有一系列触发器来控制应该上下移动的条形码。每次给出这些随机选择的值之一时,它会滑入适当的位置:

<Trigger Property="Content" Value="B000">
    <Trigger.EnterActions>
        <BeginStoryboard Name="B000" HandoffBehavior="SnapshotAndReplace">
            <Storyboard FillBehavior="HoldEnd" Duration="0:0:1">
                <DoubleAnimation Storyboard.TargetName="bar1" Storyboard.TargetProperty="Offset" To="0" Duration="0:0:1" BeginTime="0:0:0" />
                <DoubleAnimation Storyboard.TargetName="bar2" Storyboard.TargetProperty="Offset" To="0" Duration="0:0:1" BeginTime="0:0:0" />
            </Storyboard>
        </BeginStoryboard>
    </Trigger.EnterActions>
</Trigger>
<Trigger Property="Content" Value="B125">
    <Trigger.EnterActions>
        <BeginStoryboard Name="B125" HandoffBehavior="SnapshotAndReplace">
            <Storyboard FillBehavior="HoldEnd" Duration="0:0:1">
                <DoubleAnimation Storyboard.TargetName="bar1" Storyboard.TargetProperty="Offset" To=".125" Duration="0:0:1" BeginTime="0:0:0" />
                <DoubleAnimation Storyboard.TargetName="bar2" Storyboard.TargetProperty="Offset" To=".125" Duration="0:0:1" BeginTime="0:0:0" />
            </Storyboard>
        </BeginStoryboard>
    </Trigger.EnterActions>
</Trigger>
<Trigger Property="Content" Value="B250">
    <Trigger.EnterActions>
        <BeginStoryboard Name="B250" HandoffBehavior="SnapshotAndReplace">
            <Storyboard FillBehavior="HoldEnd" Duration="0:0:1">
                <DoubleAnimation Storyboard.TargetName="bar1" Storyboard.TargetProperty="Offset" To=".25" Duration="0:0:1" BeginTime="0:0:0" />
                <DoubleAnimation Storyboard.TargetName="bar2" Storyboard.TargetProperty="Offset" To=".25" Duration="0:0:1" BeginTime="0:0:0" />
            </Storyboard>
        </BeginStoryboard>
    </Trigger.EnterActions>
</Trigger>
<Trigger Property="Content" Value="B500">
    <Trigger.EnterActions>
        <BeginStoryboard Name="B500" HandoffBehavior="SnapshotAndReplace">
            <Storyboard FillBehavior="HoldEnd" Duration="0:0:1">
                <DoubleAnimation Storyboard.TargetName="bar1" Storyboard.TargetProperty="Offset" To=".5" Duration="0:0:1" BeginTime="0:0:0" />
                <DoubleAnimation Storyboard.TargetName="bar2" Storyboard.TargetProperty="Offset" To=".5" Duration="0:0:1" BeginTime="0:0:0" />
            </Storyboard>
        </BeginStoryboard>
    </Trigger.EnterActions>
</Trigger>
<Trigger Property="Content" Value="B750">
    <Trigger.EnterActions>
        <BeginStoryboard Name="B750" HandoffBehavior="SnapshotAndReplace">
            <Storyboard FillBehavior="HoldEnd" Duration="0:0:1">
                <DoubleAnimation Storyboard.TargetName="bar1" Storyboard.TargetProperty="Offset" To=".75" Duration="0:0:1" BeginTime="0:0:0" />
                <DoubleAnimation Storyboard.TargetName="bar2" Storyboard.TargetProperty="Offset" To=".75" Duration="0:0:1" BeginTime="0:0:0" />
            </Storyboard>
        </BeginStoryboard>
    </Trigger.EnterActions>
</Trigger>
<Trigger Property="Content" Value="B1k0">
    <Trigger.EnterActions>
        <BeginStoryboard Name="B1k0" HandoffBehavior="SnapshotAndReplace">
            <Storyboard FillBehavior="HoldEnd" Duration="0:0:1">
                <DoubleAnimation Storyboard.TargetName="bar1" Storyboard.TargetProperty="Offset" To="1" Duration="0:0:1" BeginTime="0:0:0" />
                <DoubleAnimation Storyboard.TargetName="bar2" Storyboard.TargetProperty="Offset" To="1" Duration="0:0:1" BeginTime="0:0:0" />
            </Storyboard>
        </BeginStoryboard>
    </Trigger.EnterActions>
</Trigger>

但由于某种原因,它只会向上移动。它应该再次向下移动的部分,它只是坐在那里。在更改之间停止动画只会导致它从底部再次开始,而这不是我想要的。

1 个答案:

答案 0 :(得分:0)

我不确定你对lol的意思,但每个动画/触发器都有EnterAction和ExitAction。

对我来说,这听起来像是在寻找ExitAction,这将允许你将那个东西移回其起始位置。 :)

看一下这个链接:

http://msdn.microsoft.com/en-us/library/system.windows.triggerbase.exitactions%28v=vs.110%29.aspx

以下是一个例子:

<Style x:Key="PropertyTriggerExampleButtonStyle" TargetType="{x:Type Button}">

  <Setter Property="Opacity" Value="0.25" />

  <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">

      <Trigger.EnterActions>
        <BeginStoryboard>
          <Storyboard>
            <DoubleAnimation Storyboard.TargetProperty="Opacity"
              To="1" Duration="0:0:1" />
          </Storyboard>
        </BeginStoryboard>
      </Trigger.EnterActions>
      <Trigger.ExitActions>
        <BeginStoryboard>
          <Storyboard>
            <DoubleAnimation Storyboard.TargetProperty="Opacity"
              To="0.25" Duration="0:0:1" />
          </Storyboard>
        </BeginStoryboard>
      </Trigger.ExitActions>          
    </Trigger>               
  </Style.Triggers>    
</Style>

修改

然后你需要有这样的多个DoubleAnimations:

        <BeginStoryboard>
          <Storyboard>
            <DoubleAnimation .... />
            <DoubleAnimation ... />
            <DoubleAnimation ... />
            <DoubleAnimation ... />
          </Storyboard>
        </BeginStoryboard>