2010-05-20 40 views
0

我目前有兩個按鈕說左右,我希望他們分別左右移動一個方形對象。如何使用silverlight左右移動物體?

當我按左時,它正在做什麼,將正方形向左移動,如果再次向左按,它會從中心向左移動。

我想要它,所以當我按左鍵時,從現在開始左右移動。

我知道原理是像獲取當前對象的位置和分別添加畫布座標,但我該怎麼做?

<UserControl x:Class="phase_2.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
d:DesignHeight="300" d:DesignWidth="400"> 
<UserControl.Resources> 
    <Storyboard x:Name="scroll_me"> 
     <DoubleAnimation x:Name="left_ani" To="0" Duration="0:0:01" Storyboard.TargetName="Rect_Animate" Storyboard.TargetProperty="(Canvas.Left)"> 
      <DoubleAnimation.EasingFunction> 
       <PowerEase EasingMode="EaseOut"></PowerEase> 
      </DoubleAnimation.EasingFunction> 
     </DoubleAnimation> 
    </Storyboard> 
    <Storyboard x:Name="scroll_me2"> 
     <DoubleAnimation x:Name="right_ani" To="200" Duration="0:0:01" Storyboard.TargetName="Rect_Animate" Storyboard.TargetProperty="(Canvas.Left)"> 
      <DoubleAnimation.EasingFunction> 
       <PowerEase EasingMode="EaseOut"></PowerEase> 
      </DoubleAnimation.EasingFunction> 
     </DoubleAnimation> 
    </Storyboard> 
</UserControl.Resources> 

<Grid x:Name="LayoutRoot" Background="White"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="1*"/> 
     <ColumnDefinition Width="2*"/> 
     <ColumnDefinition Width="1*"/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="1*"/> 
     <RowDefinition Height="35"/> 
     <RowDefinition Height="1*"/> 
    </Grid.RowDefinitions> 
    <Canvas Grid.Column="1" Grid.Row="1" x:Name="canvas1"> 
     <Rectangle x:Name="Rect_Animate" Width="35" Height="35" Fill="Red" Canvas.Left="80" /> 
    </Canvas> 
    <Button x:Name="left_btn" Width="35" Height="35" Content="L" Grid.Column="0" Grid.Row="1" Click="left_btn_Click"/> 
    <Button x:Name="right_btn" Width="35" Height="35" Content="R" Grid.Column="2" Grid.Row="1" Click="right_btn_Click"/> 
</Grid> 

+0

你能告訴我們你的代碼嗎? – ChrisF 2010-05-20 18:59:34

回答

0

只有當你的對象包含在一個畫布,這在一般不是一個偉大的佈局做法 - 這是不靈活。

如果您要將項目放置在網格中,您可以完全像Blend這樣的設計工具執行:調整要移動的任何項目的邊距。您可能有負利潤率。

+0

我把畫布放在一個網格中,同時我將動畫的對象用一個方便的方式左右移動,我仍然可以使用邊距和網格並獲得動畫效果嗎? – Craig 2010-05-20 21:20:56