[英]Animate a grid to move or slide to the right WPF
我有一個包含元素的網格。 我想移動所有這些元素,所以不可能只將網格一起移動嗎? 這對我所看到的沒有任何影響,我甚至嘗試過ScaleX等等。
<Grid x:Name="HancockDetails" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Grid.Column="1" RenderTransformOrigin="0.5,0.5">
<Rectangle HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="100"/>
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Foreground="#FFF30000"/>
</Grid>
</Grid>
</s:ScatterViewItem>
我的功能:
Storyboard sb = new Storyboard();
DoubleAnimation slide = new DoubleAnimation();
slide.To = 3000.0;
slide.From = 0;
slide.Duration = new Duration(TimeSpan.FromMilliseconds(40.0));
// Set the target of the animation
Storyboard.SetTarget(slide,HancockDetails);
Storyboard.SetTargetProperty(slide, new PropertyPath("RenderTransform.(ScaleTransform.ScaleY)"));
// Kick the animation off
sb.Children.Add(slide);
sb.Begin();
您需要先在標記中添加RenderTransform
,否則運行時無法找到它:
<Grid x:Name="HancockDetails" ...>
<Grid.RenderTransform>
<TranslateTransform />
</Grid.RenderTransform>
</Grid>
我假設您要使用TranslateTransform
,而不是ScaleTransform
,因為目標是將網格滑過,而不是調整大小?
Storyboard.SetTargetProperty(slide,
new PropertyPath("RenderTransform.(TranslateTransform.X)"));
而不是試圖手工創建故事板,這是更容易使用Expression Blend 。 它是一個免費的設計工具,與Visual Studio相得益彰 。 您正在解釋的故事板將需要30秒才能完成,並且沒有代碼。 然后當你想要它運行時它就是StoryBoardName.Begin()
為了方便嗎? 這是一種資源。 http://msdn.microsoft.com/en-us/expression/cc197141.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.