第一种方式:
XAML文件
第一步定义资源:
<UserControl.Resources>
<Storyboard x:Key="layercollapseTransition">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="BodyBack1"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)">
<EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<EasingDoubleKeyFrame KeyTime="00:00:00.2000000" Value="125"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="layerexpandTransition">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="BodyBack1"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)">
<EasingDoubleKeyFrame KeyTime="00:00:00" Value="125"/>
<EasingDoubleKeyFrame KeyTime="00:00:00.2000000" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
第二部设置关联空间
<Canvas Margin="0,0,122,16" HorizontalAlignment="Right" VerticalAlignment="Bottom" Canvas.ZIndex="200" x:Name="collapsedPane" Background="Transparent" Visibility="Collapsed">
<Grid >
<Image Height="16" Name="imageShow" Stretch="Fill" Width="16" Source="images/btn_expand.png" Opacity="0.4"
ToolTipService.ToolTip="显示图层控制" MouseLeftButtonDown="imageShow_MouseLeftButtonDown" MouseLeave="imagehide_MouseLeave" MouseEnter="imagehide_MouseEnter"
Cursor="Hand"/>
</Grid>
</Canvas>
<Canvas x:Name="BodyBack1" HorizontalAlignment="Right" VerticalAlignment="Bottom" RenderTransformOrigin="0.5,0.5" Background="Transparent" Margin="0,0,207,125">
<Border x:Name="citylayer" Background="#77919191" BorderThickness="1" CornerRadius="5"
HorizontalAlignment="Right" VerticalAlignment="Bottom"
Margin="0,0,105,0" Padding="5" BorderBrush="Black" Canvas.Left="0" Canvas.Top="7">
<StackPanel VerticalAlignment="Stretch" Width="90" HorizontalAlignment="Stretch" Canvas.Left="0">
<Image HorizontalAlignment="Left" VerticalAlignment="Top" Height="16" Name="imagehide" Stretch="Fill" Width="16" Source="images/btn_expand.png"
ToolTipService.ToolTip="隐藏图层控制"
Cursor="Hand" MouseLeftButtonDown="imagehide_MouseLeftButtonDown" MouseLeave="imagehide_MouseLeave" MouseEnter="imagehide_MouseEnter" Opacity="0.4" />
<CheckBox Content="污染物" Height="16" HorizontalAlignment="Left" Margin="16,0,0,20" Name="ckbWRWFB" VerticalAlignment="Bottom" Click="ckbWRWFB_Click" />
<CheckBox Content="污染源" Height="16" HorizontalAlignment="Left" Margin="16,0,0,20" Name="ckbWRY" VerticalAlignment="Bottom" Click="ckbWRY_Click" />
<CheckBox Content="风向风速" Height="16" HorizontalAlignment="Left" Margin="16,0,0,0" Name="ckbFXFS" VerticalAlignment="Bottom" Click="ckbFXFS_Click" />
</StackPanel>
</Border>
<Canvas.RenderTransform>
<TranslateTransform />
</Canvas.RenderTransform>
</Canvas>
第三步关联代码
private void imagehide_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var collapseAnimation = (Storyboard)Resources["layercollapseTransition"];//获取故事板
collapseAnimation.Begin();//隐藏故事板开始执行
collapsedPane.Visibility = Visibility.Visible;//展开面板可见
}
private void imagehide_MouseLeave(object sender, MouseEventArgs e)
{
((Image)sender).Opacity = 0.4;//设置图片透明度
}
private void imagehide_MouseEnter(object sender, MouseEventArgs e)
{
((Image)sender).Opacity = 1;//设置图片透明度
}
private void imageShow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var collapseAnimation = (Storyboard)Resources["layerexpandTransition"];//获取展开的故事板
collapseAnimation.Begin();//故事板开始执行
BodyBack1.Visibility = Visibility.Visible;//图层控制面板可见
collapsedPane.Visibility = Visibility.Collapsed;//展开面板不可见
}
第二种方式:(纯代码)
/// <summary>
/// SetHideAction() 设置隐藏效果
/// </summary>
/// <param name="Pcanvas">容器</param>
/// <param name="PPropertypath">属性路径</param>
/// <param name="Pwidthorheight">高度或宽度</param>
/// <param name="Plong">持续时长</param>
/// <returns></returns>
public void SetHideAction(Canvas Pcanvas, string PPropertypath, int Pwidthorheight, double Plong)
{
Storyboard keyFrameboard = new Storyboard();
DoubleAnimationUsingKeyFrames dakeyframe = new DoubleAnimationUsingKeyFrames();
//设置rect矩形控件的Opacity透明度,并且开始动画事件为0秒的时候。
Storyboard.SetTarget(dakeyframe, Pcanvas);
Storyboard.SetTargetProperty(dakeyframe, new PropertyPath(PPropertypath));
dakeyframe.BeginTime = new TimeSpan(0, 0, 0);//设置开始时间
//添加一个在第二秒的时候Opacity透明度值为1的关键帧
SplineDoubleKeyFrame SKeyFrame = new SplineDoubleKeyFrame();
SKeyFrame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0));
SKeyFrame.Value = 0;
dakeyframe.KeyFrames.Add(SKeyFrame);
//添加一个在第四秒的时候Opacity透明度值为0.2的关键帧
SplineDoubleKeyFrame SKeyFrame1 = new SplineDoubleKeyFrame();
SKeyFrame1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(Plong));
SKeyFrame1.Value = Pwidthorheight;
dakeyframe.KeyFrames.Add(SKeyFrame1);
keyFrameboard.Children.Add(dakeyframe);
keyFrameboard.Begin();//隐藏故事板开始执行
}
/// <summary>
/// SetShowAction() 设置显示效果
/// </summary>
/// <param name="Pcanvas">容器</param>
/// <param name="PPropertypath">属性路径</param>
/// <param name="Pwidthorheight">高度或宽度</param>
/// <param name="Plong">持续时长</param>
/// <returns></returns>
public void SetShowAction(Canvas Pcanvas, string PPropertypath, int Pwidthorheight, double Plong)
{
Storyboard keyFrameboard = new Storyboard();
DoubleAnimationUsingKeyFrames dakeyframe = new DoubleAnimationUsingKeyFrames();
//设置rect矩形控件的Opacity透明度,并且开始动画事件为0秒的时候。
Storyboard.SetTarget(dakeyframe, Pcanvas);
Storyboard.SetTargetProperty(dakeyframe, new PropertyPath(PPropertypath));
dakeyframe.BeginTime = new TimeSpan(0, 0, 0);//设置开始时间
//添加一个在第二秒的时候Opacity透明度值为1的关键帧
SplineDoubleKeyFrame SKeyFrame = new SplineDoubleKeyFrame();
SKeyFrame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0));
SKeyFrame.Value = Pwidthorheight;
dakeyframe.KeyFrames.Add(SKeyFrame);
//添加一个在第四秒的时候Opacity透明度值为0.2的关键帧
SplineDoubleKeyFrame SKeyFrame1 = new SplineDoubleKeyFrame();
SKeyFrame1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(Plong));
SKeyFrame1.Value = 0;
dakeyframe.KeyFrames.Add(SKeyFrame1);
keyFrameboard.Children.Add(dakeyframe);
keyFrameboard.Begin();//隐藏故事板开始执行
}