一 平移
1、 xaml代码
-----在资源中定义动画效果------
<UserControl.Resources>
<!--出场动画-->
<Storyboard x:Key="InTransition">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="gbSearch" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<SplineDoubleKeyFrame KeyTime="00:00:02" Value="0"/>
<SplineDoubleKeyFrame KeyTime="00:00:02.5000000" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="gbSearch" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="-50"/>
<SplineDoubleKeyFrame KeyTime="00:00:02" Value="-101"/>
<SplineDoubleKeyFrame KeySpline="0,0,0,1" KeyTime="00:00:03.5000000" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="gbAdd" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<SplineDoubleKeyFrame KeyTime="00:00:01" Value="0"/>
<SplineDoubleKeyFrame KeyTime="00:00:01.5000000" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="gbAdd" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="-72"/>
<SplineDoubleKeyFrame KeyTime="00:00:01" Value="-157"/>
<SplineDoubleKeyFrame KeySpline="0.5,0,0.5,1" KeyTime="00:00:01.5000000" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
------定义触发的事件----------
<UserControl.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource InTransition}"/>
</EventTrigger>
</UserControl.Triggers>
------应用效果的两个控件--------
<GroupBox Header="违章信息查询" Name="gbSearch" BorderBrush="LightBlue" Foreground="Blue">
<GroupBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</GroupBox.RenderTransform>
</GroupBox>
<GroupBox Header="违章车辆信息采集" Name="gbAdd" BorderBrush="LightBlue" Foreground="Blue">
<GroupBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</GroupBox.RenderTransform>
</GroupBox>
蓝色xaml说明:所应用效果的控件必须要些否则会出岔子
2、C# 后台代码控制效果
在前台xaml中设置spDataGrid的Visibility="Collapsed";
后台代码如下:
this.spDataGrid.Visibility = Visibility.Visible;
Storyboard myStoryboard = new Storyboard();
DoubleAnimation OpacityDoubleAnimation = new DoubleAnimation();
OpacityDoubleAnimation.From = 0;
OpacityDoubleAnimation.To = 1;
OpacityDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.4));
Storyboard.SetTargetName(OpacityDoubleAnimation, spDataGrid.Name);
Storyboard.SetTargetProperty(OpacityDoubleAnimation, new PropertyPath(DataGrid.OpacityProperty));
spDataGrid.RenderTransform = new TranslateTransform();
DependencyProperty[] propertyChain = new DependencyProperty[]
{
DataGrid.RenderTransformProperty,
TranslateTransform.XProperty
};
DoubleAnimation InDoubleAnimation = new DoubleAnimation();
InDoubleAnimation.From = -200;
InDoubleAnimation.To = 0;
InDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.4));
Storyboard.SetTargetName(InDoubleAnimation, spDataGrid.Name);
Storyboard.SetTargetProperty(InDoubleAnimation, new PropertyPath("(0).(1)", propertyChain));
myStoryboard.Children.Add(OpacityDoubleAnimation);