2013-07-12 103 views

回答

1

你可以团例如栅格在容器中的按钮,然后动画容器。

另一种方法是将动画放入样式中,并将样式应用于按钮,或者通过设置每个按钮的样式显式设置,或者通过在按钮的父级中设置按钮的默认样式来隐式设置样式容器)

下面是一个使用样式的例子。

不幸的是,窗口的加载事件被一再呼吁让动画不断持续。我看到的唯一解决方案是在第一次后设置布尔值,并防止动画再次出现。

<Window x:Class="WpfApplication3.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" 
     Height="350" 
     Width="525"> 
    <Grid> 
     <Grid HorizontalAlignment="Center" 
       VerticalAlignment="Center"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="auto" /> 
       <RowDefinition Height="auto" /> 
       <RowDefinition Height="auto" /> 
      </Grid.RowDefinitions> 
      <Grid.Resources> 
       <Style TargetType="Button"> 
        <Setter Property="Width" 
          Value="150" /> 
        <Setter Property="Padding" 
          Value="15" /> 
        <Setter Property="Margin" 
          Value="15" /> 
        <Setter Property="RenderTransform"> 
         <Setter.Value> 
          <TranslateTransform X="0" /> 
         </Setter.Value> 
        </Setter> 
        <Style.Triggers> 
         <EventTrigger RoutedEvent="Window.Loaded"> 
          <BeginStoryboard> 
           <Storyboard FillBehavior="HoldEnd"> 
            <DoubleAnimation Storyboard.TargetProperty="(Button.RenderTransform).(TranslateTransform.X)" 
                From="-150" 
                To="0" 
                RepeatBehavior="1" 
                Duration="0:0:2" /> 
           </Storyboard> 
          </BeginStoryboard> 
         </EventTrigger> 
        </Style.Triggers> 
       </Style> 
      </Grid.Resources> 
      <Button Grid.Row="0">Start</Button> 
      <Button Grid.Row="1">Setup</Button> 
      <Button Grid.Row="2">Quit</Button> 

     </Grid> 
    </Grid> 
</Window> 
+0

我用网格!谢谢它的作品! –

+0

我添加了一个使用样式的例子。不幸的是,窗口的加载事件反复被调用,所以动画继续进行。我看到的唯一解决方案是在第一次后设置布尔值,并防止动画再次出现。 –

+0

谢谢!然而,在风格和网格之间有几个问题,哪一个可以为动画提供更多选择?什么时候建议在网格上使用样式,反之亦然? –

相关问题