WPF中自定义GridLengthAnimation
发布时间 - 2026-01-11 01:12:30 点击率:次需求

我们想在编辑一个列表中某一个条目时,将编辑的详情内容也放置当前面,比如右侧。
可以通过将一个Grid,分成两个Cloumn,动态调整两个Cloumn的Width,就可以实现这个需求。
我们知道,Clomun的Width是个,而默认的动画没有这样子的。我们就需要自己实现这样一人动画。
设计
我们从Animation的类图上看到
我们可以从需求
我们想在编辑一个列表中某一个条目时,将编辑的详情内容也放置当前面,比如右侧。
可以通过将一个Grid,分成两个Cloumn,动态调整两个Cloumn的Width,就可以实现这个需求。
我们知道,Clomun的Width是个GridLength,而默认的动画没有这样子的。我们就需要自己实现这样一人动画。
设计
我们从Animation的类图上看到AnimationTimeline继承,重写其GetCurrentValue
public class GridLengthAnimation : AnimationTimeline
{
/// <summary>
/// Returns the type of object to animate
/// </summary>
public override Type TargetPropertyType => typeof(GridLength);
/// <summary>
/// Creates an instance of the animation object
/// </summary>
/// <returns>Returns the instance of the GridLengthAnimation</returns>
protected override System.Windows.Freezable CreateInstanceCore()
{
return new GridLengthAnimation();
}
/// <summary>
/// Dependency property for the From property
/// </summary>
public static readonly DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(GridLength),
typeof(GridLengthAnimation));
/// <summary>
/// CLR Wrapper for the From depenendency property
/// </summary>
public GridLength From
{
get
{
return (GridLength)GetValue(GridLengthAnimation.FromProperty);
}
set
{
SetValue(GridLengthAnimation.FromProperty, value);
}
}
/// <summary>
/// Dependency property for the To property
/// </summary>
public static readonly DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(GridLength),
typeof(GridLengthAnimation));
/// <summary>
/// CLR Wrapper for the To property
/// </summary>
public GridLength To
{
get
{
return (GridLength)GetValue(GridLengthAnimation.ToProperty);
}
set
{
SetValue(GridLengthAnimation.ToProperty, value);
}
}
/// <summary>
/// Animates the grid let set
/// </summary>
/// <param name="defaultOriginValue">The original value to animate</param>
/// <param name="defaultDestinationValue">The final value</param>
/// <param name="animationClock">The animation clock (timer)</param>
/// <returns>Returns the new grid length to set</returns>
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);
}
如上所示,我们仿着默认动画实现了From,To,同时将其属性定义为GridLength,当动画执行时,我们重写了GetCurrentValue,使其根据From/To属性相关联。
优化
通过以上代码,我们实现了在GridLength变化时,实现动画。但是,试用后我们发现,动画,有点太线性。这个时候,怎么办?
可以通过引入EasingFunction来实现。我们知道EasingFunction其实就是一个与时间t有关的时间函数f(t).通过时间函数的处理,我们使动画过渡不要那么线性。
/// <summary>
/// The <see cref="EasingFunction" /> dependency property's name.
/// </summary>
public const string EasingFunctionPropertyName = "EasingFunction";
/// <summary>
/// Gets or sets the value of the <see cref="EasingFunction" />
/// property. This is a dependency property.
/// </summary>
public IEasingFunction EasingFunction
{
get
{
return (IEasingFunction)GetValue(EasingFunctionProperty);
}
set
{
SetValue(EasingFunctionProperty, value);
}
}
/// <summary>
/// Identifies the <see cref="EasingFunction" /> dependency property.
/// </summary>
public static readonly DependencyProperty EasingFunctionProperty = DependencyProperty.Register(
EasingFunctionPropertyName,
typeof(IEasingFunction),
typeof(GridLengthAnimation),
new UIPropertyMetadata(null));
对应的,还要重写GetCurrentValue函数。
public override object GetCurrentValue(object defaultOriginValue,
object defaultDestinationValue, AnimationClock animationClock)
{
double fromVal = ((GridLength)GetValue(FromProperty)).Value;
double toVal = ((GridLength)GetValue(ToProperty)).Value;
//check that from was set from the caller
//if (fromVal == 1)
// //set the from as the actual value
// fromVal = ((GridLength)defaultDestinationValue).Value;
double progress = animationClock.CurrentProgress.Value;
IEasingFunction easingFunction = EasingFunction;
if (easingFunction != null)
{
progress = easingFunction.Ease(progress);
}
if (fromVal > toVal)
return new GridLength((1 - progress) * (fromVal - toVal) + toVal, GridUnitType.Star);
return new GridLength(progress * (toVal - fromVal) + fromVal, GridUnitType.Star);
}
使用
<anim:GridLengthAnimation Storyboard.TargetProperty="Width" From="0" To="*" Duration="0:0:0.5"/>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# WPF
# GridLengthAnimation
# WPF弹出自定义窗口的方法
# WPF的ListView控件自定义布局用法实例
# WPF 自定义雷达图开发实例教程
# WPF自定义搜索框代码分享
# WPF自定义选择年月控件详解
# WPF自定义控件和样式之自定义按钮(Button)
# WPF如何自定义TabControl控件样式示例详解
# WPF自定义TreeView控件样式实现QQ联系人列表效果
# 可以通过
# 是个
# 一人
# 重写
# 这样子
# 就可以
# 图上
# 实现了
# 列表中
# 将其
# 我们可以
# 写了
# 这个时候
# 使其
# 相关联
# 所示
# 来实现
# 大家多多
# Type
# override
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
EditPlus中的正则表达式实战(5)
网站设计制作书签怎么做,怎样将网页添加到书签/主页书签/桌面?
javascript和jQuery中的AJAX技术详解【包含AJAX各种跨域技术】
,网页ppt怎么弄成自己的ppt?
JS碰撞运动实现方法详解
如何在阿里云购买域名并搭建网站?
Laravel如何生成和使用数据填充?(Seeder和Factory示例)
广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?
如何快速查询网址的建站时间与历史轨迹?
哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?
免费网站制作appp,免费制作app哪个平台好?
如何使用 Go 正则表达式精准提取括号内首个纯字母标识符(忽略数字与嵌套)
零基础网站服务器架设实战:轻量应用与域名解析配置指南
Win11怎么关闭专注助手 Win11关闭免打扰模式设置【操作】
如何在IIS中配置站点IP、端口及主机头?
Edge浏览器提示“由你的组织管理”怎么解决_去除浏览器托管提示【修复】
Win11怎么关闭透明效果_Windows11辅助功能视觉效果设置
Laravel如何使用Eloquent进行子查询
香港服务器网站推广:SEO优化与外贸独立站搭建策略
Zeus浏览器网页版官网入口 宙斯浏览器官网在线通道
Laravel观察者模式如何使用_Laravel Model Observer配置
Laravel如何实现RSS订阅源功能_Laravel动态生成网站XML格式订阅内容【教程】
悟空浏览器如何设置小说背景色_悟空浏览器背景色设置【方法】
在centOS 7安装mysql 5.7的详细教程
敲碗10年!Mac系列传将迎来「触控与联网」双革新
EditPlus中的正则表达式 实战(1)
音响网站制作视频教程,隆霸音响官方网站?
如何登录建站主机?访问步骤全解析
Internet Explorer官网直接进入 IE浏览器在线体验版网址
PHP怎么接收前端传的文件路径_处理文件路径参数接收方法【汇总】
Laravel如何配置和使用队列处理异步任务_Laravel队列驱动与任务分发实例
Laravel怎么判断请求类型_Laravel Request isMethod用法
详解Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点
如何彻底删除建站之星生成的Banner?
怎么制作一个起泡网,水泡粪全漏粪育肥舍冬季氨气超过25ppm,可以有哪些措施降低舍内氨气水平?
夸克浏览器网页跳转延迟怎么办 夸克浏览器跳转优化
详解Huffman编码算法之Java实现
Laravel怎么多语言本地化设置_Laravel语言包翻译与Locale动态切换【手册】
中山网站推广排名,中山信息港登录入口?
如何快速上传建站程序避免常见错误?
Laravel中的Facade(门面)到底是什么原理
Laravel如何与Vue.js集成_Laravel + Vue前后端分离项目搭建指南
网站图片在线制作软件,怎么在图片上做链接?
html5如何设置样式_HTML5样式设置方法与CSS应用技巧【教程】
C++时间戳转换成日期时间的步骤和示例代码
黑客如何利用漏洞与弱口令入侵网站服务器?
网页制作模板网站推荐,网页设计海报之类的素材哪里好?
动图在线制作网站有哪些,滑动动图图集怎么做?
php8.4header发送头信息失败怎么办_php8.4header函数问题解决【解答】
如何选择可靠的免备案建站服务器?

