动画网格以移动或滑动到右侧 WPF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17753506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Animate a grid to move or slide to the right WPF
提问by user99999991
I have a grid with elements in it. I want to move all of those elements, so is it not possible to just move the grid all together? This isn't having any effect from what I'm seeing and I've tried even ScaleX and such as well.
我有一个包含元素的网格。我想移动所有这些元素,所以不能一起移动网格吗?这对我所看到的没有任何影响,我什至尝试过 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>
My function:
我的功能:
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();
采纳答案by McGarnagle
You need to first add the RenderTransformin the markup, otherwise the runtime can't find it:
需要先RenderTransform在标记中添加,否则运行时找不到:
<Grid x:Name="HancockDetails" ...>
<Grid.RenderTransform>
<TranslateTransform />
</Grid.RenderTransform>
</Grid>
I assume you want to use TranslateTransform, and not ScaleTransform, since the goal is to slide the grid over, not to resize it?
我假设您想使用TranslateTransform,而不是ScaleTransform,因为目标是将网格滑过,而不是调整它的大小?
Storyboard.SetTargetProperty(slide,
new PropertyPath("RenderTransform.(TranslateTransform.X)"));
回答by Anthony Russell
Instead of trying to create a storyboard by hand it is muuuuuuch easier to use Expression Blend. Its a free design tool that compliments Visual Studio. The storyboard you are explaining would take all of 30 seconds to make there and there would be no code. Then when you wanted it to run it would just be the StoryBoardName.Begin()
使用Expression Blend比尝试手动创建故事板要容易得多。它是一种免费的设计工具,可与Visual Studio. 您正在解释的故事板需要 30 秒才能制作完成,而且没有代码。然后,当您希望它运行时,它只是StoryBoardName.Begin()
To easy right? Heres a resource. http://msdn.microsoft.com/en-us/expression/cc197141.aspx
容易吧?这是一个资源。http://msdn.microsoft.com/en-us/expression/cc197141.aspx

