[答网友问]让GridLength支持动画
2024-08-25 07:38:21
[答网友问]WPF中让GridLength类型支持动画
周银辉
今天一位收到网友求助:
"你好!我想向你请教关于动画的问题:比如,一个Page分为上、下l两块,我想通过动画实现分别隐藏某块的内容,当我触发一个按钮的Click事件时,实现隐藏上面的面板,同时下的面板就要延伸并占据上面面板的空间,这个过程到好实现,问题是我如果第二次触发这个按钮的Click事件时,怎么才能让上面的面板出现(恢复原始大小)并且下面的面板的大小也回到原始大小"
对于这个问题,有一个很好的解决方法是,将两个面板放到Grid中,并让GridLength类型支持动画,就像Double类型有着对应的DoubleAnimation一样.这样就将网友的问题转化为:上面的面板所在行对应RowDefinition的高度由0.5变为0,再由0变为0.5(单位GridUnitType.Star)
以下是GridLengthAnimation类的完整代码,你可以使用她就像使用DoubleAnimaion一样.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media.Animation;
using System.Windows;
using System.Diagnostics;
namespace GridAnimationDemo
{
internal class GridLengthAnimation : AnimationTimeline
{
public static readonly DependencyProperty FromProperty;
public static readonly DependencyProperty ToProperty;
static GridLengthAnimation()
{
FromProperty = DependencyProperty.Register("From", typeof(GridLength),
typeof(GridLengthAnimation));
ToProperty = DependencyProperty.Register("To", typeof(GridLength),
typeof(GridLengthAnimation));
}
public override Type TargetPropertyType
{
get
{
return typeof(GridLength);
}
}
protected override System.Windows.Freezable CreateInstanceCore()
{
return new GridLengthAnimation();
}
public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock)
{
double fromVal = ((GridLength)GetValue(GridLengthAnimation.FromProperty)).Value;
double toVal = ((GridLength)GetValue(GridLengthAnimation.ToProperty)).Value;
if (fromVal > toVal)
{
return new GridLength((1 - animationClock.CurrentProgress.Value) * (fromVal - toVal) + toVal, GridUnitType.Star);
}
else
return new GridLength(animationClock.CurrentProgress.Value * (toVal - fromVal) + fromVal, GridUnitType.Star);
}
public GridLength From
{
get
{
return (GridLength)GetValue(GridLengthAnimation.FromProperty);
}
set
{
SetValue(GridLengthAnimation.FromProperty, value);
}
}
public GridLength To
{
get
{
return (GridLength)GetValue(GridLengthAnimation.ToProperty);
}
set
{
SetValue(GridLengthAnimation.ToProperty, value);
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Media.Animation;
using System.Windows;
using System.Diagnostics;
namespace GridAnimationDemo
{
internal class GridLengthAnimation : AnimationTimeline
{
public static readonly DependencyProperty FromProperty;
public static readonly DependencyProperty ToProperty;
static GridLengthAnimation()
{
FromProperty = DependencyProperty.Register("From", typeof(GridLength),
typeof(GridLengthAnimation));
ToProperty = DependencyProperty.Register("To", typeof(GridLength),
typeof(GridLengthAnimation));
}
public override Type TargetPropertyType
{
get
{
return typeof(GridLength);
}
}
protected override System.Windows.Freezable CreateInstanceCore()
{
return new GridLengthAnimation();
}
public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock)
{
double fromVal = ((GridLength)GetValue(GridLengthAnimation.FromProperty)).Value;
double toVal = ((GridLength)GetValue(GridLengthAnimation.ToProperty)).Value;
if (fromVal > toVal)
{
return new GridLength((1 - animationClock.CurrentProgress.Value) * (fromVal - toVal) + toVal, GridUnitType.Star);
}
else
return new GridLength(animationClock.CurrentProgress.Value * (toVal - fromVal) + fromVal, GridUnitType.Star);
}
public GridLength From
{
get
{
return (GridLength)GetValue(GridLengthAnimation.FromProperty);
}
set
{
SetValue(GridLengthAnimation.FromProperty, value);
}
}
public GridLength To
{
get
{
return (GridLength)GetValue(GridLengthAnimation.ToProperty);
}
set
{
SetValue(GridLengthAnimation.ToProperty, value);
}
}
}
}
最新文章
- PHP验证用户登录例子-学习笔记
- Python数据分析
- Android动画的理解
- <;head>;中<;meta name=";viewport"; content=";width=device-width,initical-scale=1";的作用>;
- 【文件】读取一个文件夹下所有的jpg图片
- [游戏模版20] Win32 物理引擎 加速运动
- CoreLoation
- leetcode 题解: Length of Last Word
- 如何让centos6.5在vm11里上网,连接网络?
- Android Environment 判断sd卡是否挂载 获取sd卡目录
- 【转】ubuntu14.04 trusty的源
- 与我一起extjs5(09--其定义菜单2)
- java中的异常处理机制
- json生成方式
- 白瑜庆:知乎基于Kubernetes的kafka平台的设计和实现
- python正则表达式基础,以及pattern.match(),re.match(),pattern.search(),re.search()方法的使用和区别
- HDU 3949 XOR [线性基|高斯消元]
- kettle 在javascrip代码组件中使用fireToDB()函数实现自定义数据库查询
- C#简单画图程序
- MongoDB学习记录(一) - 安装、启动与建立数据库
热门文章
- IOS 公司标示使用反向域名
- 还是一个关于c++内存指针的问题分析
- Uva 11468 AC自动机或运算
- SSH Secure Shell Client连接Linux断开
- Android学习笔记_22_服务Service应用之—与Activity进行相互通信的本地服务
- js中的AJAX
- 分组函数group by和Oracle中分析函数partition by的用法以及区别
- Oracle数据库中 to_date()与to_char()函数的用法
- Java实现批量修改文件名,重命名
- App 分辨率相关 - iOS