[答网友问]让GridLength支持动画
[答网友问]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);
}
}

}
}
[答网友问]让GridLength支持动画的更多相关文章
- iOS图案锁,支持动画、图片、绘图
最近忙着搭建一个聊天用的框架,过几天应该会整理写出来吧,原理不难,但是实现后会省很多事.好久没写博客,周末心血来潮写了个图案锁,这东西没什么技术含量,网上一堆,这次这个图案锁顺便联系了怎么打包使用.a ...
- 答CsdnBlogger问-关于安卓入行和开发问题
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 问1:请问大牛对功能和框架的认识有哪些?(提问者:执笔记忆的空白) 比如对于一个小公司来说,什么样的 ...
- 「脑洞」图片转HTML(支持动画)
也许是受到很久以前看到的这玩意儿的原因:The Shapes of CSS 现在开脑洞写了个自动转换,顺便支持了动画……嗯,纯 CSS (:з」∠) 主要步骤就是用 Python 读图片,然后把像素全 ...
- 答CsdnBlogger问-关于职业发展和团队管理问题
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 问1:关于职业发展以及团队管理?(正能同學_) 请问在二线城市的小公司里,普通Android开发者的 ...
- 答CsdnBlogger问-关于定时和后台服务问题
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 前段时间写了不少博客,在问答页面也陆续回答几十个问题,之后Csdn乙同学找到我,说要推荐我参加问答类 ...
- css3支持动画吗?css3可以用于网页动画的展现吗
CSS3 主要可以分为几个模块:边框和背景,渐变,文字特效,字体,2D/3D转换,动画(过渡动画和动画),选择器,盒模型,多列布局,用户界面. css3动画有2类:一种是transition的,另一种 ...
- 基于图像切换器(imageSwitcher)的支持动画的图片浏览器
利用GridView和ImageSwitcher的基本用法 public class MainActivity extends Activity { int[] imageIds = new int[ ...
- 答读者问(6):有关IT培训和毕业之前的迷茫等问题
近期在微博上与一些读者朋友们交流,发现大家对自己的未来都比較的关心.有些朋友认为在大学里面没有学到什么东西,问我要不要到一些IT培训机构去"速成".另一些朋友即将毕业,不知道自己走 ...
- 答读者问(1):非模式物种找marker;如何根据marker定义细胞类型
下午花了两个小时回答读者的疑问,觉得可以记录下来,也许能帮到一部分人. 第一位读者做的是非模式物种的单细胞. 一开始以为是想问我非模式物种的marker基因在哪儿找,读者朋友也提到了blast 研究的 ...
随机推荐
- Scrum _GoodJob
作为长大的大三老腊肉,我们已经在长大生活了两年多,对于什么是长大人最想完善的校园需求.最想拥有的校园服务媒介也有了更加深切的体会. 于是,GoodJob小团队blingbling闪现啦!! GoodJ ...
- jsonp跨域请求响应结果处理函数(python)
接口测试跨域请求接口用的jsonp,需要将回调函数里的json字符串提取出来. jsonp跨域请求的响应结果格式: callback_functionname(json字符串). #coding:ut ...
- IOS ASI 请求服务器 总结
一.发送请求的2个对象 1.发送GET请求:ASIHttpRequest 2.发送POST请求:ASIFormDataRequest* 设置参数// 同一个key只对应1个参数值,适用于普通“单值参数 ...
- libevent-select模型分析
下面内容为windows下select模型分析,原博客链接 http://blog.csdn.net/fish_55_66/article/details/50352080 https://www.c ...
- ArcGIS Server 10 Java 版的Rest服务的部署方法
使用ArcGIS Server 10 Java版发布GIS服务,当使用ArcGIS Manager创建好服务后,然后打开“ArcGIS Services Directory”的链接时发现网页报出了找不 ...
- Going deeper with convolutions(GoogLeNet、Inception)
从LeNet-5开始,cnn就有了标准的结构:stacked convolutional layers are followed by one or more fully-connected laye ...
- webstorn中的vue文件识别es6代码
webstorn中的vue文件识别es6代码 1.webstorm中es6语法报错,解决方法: 打开 Settings => Languages & Frameworks => J ...
- 推荐一个zookeeper信息查看工具
zookeeper信息查看工具 下载地址:https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip 解压,打 ...
- git报错error: src refspec refs/heads/master does not match any.
$ git pusherror: src refspec refs/heads/master does not match any.error: failed to push some refs 出错 ...
- 简单的使用git克隆上传创建下载删除
首先我们下载个git 官网:https://git-scm.com/ 我这里下的是64-bit windows setup 下载完成后安装: 一直下一步就好 安装完成后进行打开! 现在我们需要去官网注 ...