2013-07-18 86 views

回答

0

那麼,這取決於你的實際佈局:你想在網格或畫布中設置動畫按鈕(可以爲Margin屬性或Canvas設置動畫。左附屬性,分別)?你想要動畫屬性本身還是變形(後者會渲染RenderTransform - 特別是TranslateTransform)。如果您仍想引用「舊」位置,則可以使用RenderTransform。

一個簡單的方法是:

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Grid> 
    <Grid.Triggers> 
     <EventTrigger RoutedEvent="Grid.Loaded"> 
      <BeginStoryboard> 
       <Storyboard RepeatBehavior="Forever"> 
        <DoubleAnimation Storyboard.TargetName="myButton" 
         Storyboard.TargetProperty="(Canvas.Left)" From="1" To="350" 
         Duration="0:0:10" BeginTime="0:0:0"/> 
       </Storyboard> 
      </BeginStoryboard> 
     </EventTrigger> 
    </Grid.Triggers> 
    <Canvas x:Name="myCanvas" Background="Yellow"> 
     <Button x:Name="myButton" Width="100" Height="30" Canvas.Left="100" Canvas.Top="100" /> 
    </Canvas> 
    </Grid> 
</Window> 
0

它會更好,如果你使用的共混物..我已經genrated爲向右..just檢查它的StackPanel移動代碼的故事板..

you can go through this video aslo it's very good它會完美工作你的情況

<Page.Resources> 
    <Storyboard x:Name="Storyboard1"> 
     <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="hello"> 
      <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
      <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="100"/> 
     </DoubleAnimationUsingKeyFrames> 
    </Storyboard> 
</Page.Resources> 

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> 
    <StackPanel Name="hello" Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" > 
     <StackPanel.RenderTransform> 
      <CompositeTransform/> 
     </StackPanel.RenderTransform> 
     <TextBlock Text="hello1" FontSize="50" /> 
     <Button Content="Button" FontSize="50" Click="Button_Click_1" /> 
    </StackPanel> 
</Grid> 

,並開始做這個按鈕點擊..

private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     Storyboard1.Begin(); 
    } 

爲更好地理解只讀瞭如何使用混合..