wpf 滑入和滑出动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33114198/
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
Slide in & Slide out animation
提问by Fernando Santiago
I am trying to make a slide in & out animation with c# & WPF.
我正在尝试使用 c# 和 WPF 制作滑入和滑出动画。
So far I have managed to code the next lines:
到目前为止,我已经设法编写了下一行代码:
XAML:
XAML:
<Grid Name="grid" Grid.Column="0" Grid.Row="1">
<Border Name="border" Background="Red"></Border>
</Grid>
c#:
C#:
private void Button_Click(object sender, RoutedEventArgs e) {
border.Height = grid.ActualHeight;
if (!isOpen) {
isOpen = true;
DoubleAnimation db = new DoubleAnimation();
db.From = 0;
db.To = grid.ActualHeight;
db.Duration = TimeSpan.FromSeconds(0.5);
border.BeginAnimation(Grid.HeightProperty, db);
} else {
isOpen = false;
DoubleAnimation db = new DoubleAnimation();
db.From = grid.ActualHeight;
db.To = 0;
db.Duration = TimeSpan.FromSeconds(0.5);
border.BeginAnimation(Grid.HeightProperty, db);
}
}
The good thing is that the animation is executed. The bad thing is that this animation has the wrong effect, i mean the animation goes from top to middle and bottom to middle (like if it was shrinking)...
好消息是动画被执行了。不好的是这个动画有错误的效果,我的意思是动画从上到中,从下到中(就像它在缩小一样)......
How can i make (or modify in my actual code) the slide effect(top to bottom & bottom to top)?
我如何制作(或在我的实际代码中修改)幻灯片效果(从上到下和从下到上)?
It has to be in c# code.
它必须在 c# 代码中。
回答by BradleyDotNET
You are trying to translateyour UI control so use a TranslateTransform(Canvas.Topis possible if you are on a canvas, but inefficient).
您正在尝试翻译您的 UI 控件,因此使用TranslateTransform(Canvas.Top如果您在画布上是可能的,但效率低下)。
Modify your XAML to include a render transform set to a TranslateTransformobject:
修改您的 XAML 以包含设置为TranslateTransform对象的渲染转换:
<Grid Name="grid" Grid.Column="0" Grid.Row="1" ClipToBounds="true">
<Border Name="border" Background="Red">
<Border.RenderTransform>
<TranslateTransform x:Name="borderTransform"/>
</Border.RenderTransform>
</Border>
</Grid>
And animate the Yproperty of the transform:
并Y为变换的属性设置动画:
DoubleAnimation db = new DoubleAnimation();
db.From = 0;
db.To = grid.ActualHeight;
db.Duration = TimeSpan.FromSeconds(0.5);
borderTransform.BeginAnimation(TranslateTransform.YProperty, db);
Just so you know, doing this is a wholelot cleaner using a Storyboardobject (plus you can set it up in XAML!)
只要你知道,这样做是一个整体使用了大量清洁Storyboard的对象(再加上你可以设置它在XAML!)

