需求

我们想在编辑一个列表中某一个条目时,将编辑的详情内容也放置当前面,比如右侧。
可以通过将一个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
相关文章:
宝塔Windows建站如何避免显示默认IIS页面?
如何通过虚拟机搭建网站?详细步骤解析
济南企业网站制作公司,济南社保单位网上缴费步骤?
定制建站价位费用解析与套餐推荐全攻略
小型网站制作HTML,*游戏网站怎么搭建?
如何在万网ECS上快速搭建专属网站?
如何用VPS主机快速搭建个人网站?
如何在阿里云完成域名注册与建站?
如何快速搭建FTP站点实现文件共享?
如何配置IIS站点权限与局域网访问?
高端网站建设与定制开发一站式解决方案 中企动力
如何快速建站并高效导出源代码?
如何获取开源自助建站系统免费下载链接?
在线教育网站制作平台,山西立德教育官网?
建站之星如何防范黑客攻击与数据泄露?
,有什么在线背英语单词效率比较高的网站?
如何安全更换建站之星模板并保留数据?
建站之星3.0如何解决常见操作问题?
如何通过远程VPS快速搭建个人网站?
香港代理服务器配置指南:高匿IP选择、跨境加速与SEO优化技巧
如何规划企业建站流程的关键步骤?
怎么制作一个起泡网,水泡粪全漏粪育肥舍冬季氨气超过25ppm,可以有哪些措施降低舍内氨气水平?
股票网站制作软件,网上股票怎么开户?
如何高效配置IIS服务器搭建网站?
建站主机如何选?性能与价格怎样平衡?
简易网站制作视频教程,使用记事本编写一个简单的网页html文件?
济南网站建设制作公司,室内设计网站一般都有哪些功能?
如何在云主机上快速搭建多站点网站?
专业商城网站制作公司有哪些,pi商城官网是哪个?
如何制作一个表白网站视频,关于勇敢表白的小标题?
建站之星后台密码遗忘如何找回?
如何快速打造个性化非模板自助建站?
图册素材网站设计制作软件,图册的导出方式有几种?
网站企业制作流程,用什么语言做企业网站比较好?
实现虚拟支付需哪些建站技术支撑?
如何在腾讯云免费申请建站?
如何在阿里云部署织梦网站?
宁波自助建站系统如何快速打造专业企业网站?
深圳网站制作案例,网页的相关名词有哪些?
如何将凡科建站内容保存为本地文件?
如何在Tomcat中配置并部署网站项目?
Swift中循环语句中的转移语句 break 和 continue
公众号网站制作网页,微信公众号怎么制作?
如何快速搭建支持数据库操作的智能建站平台?
如何正确选择百度移动适配建站域名?
如何选择香港主机高效搭建外贸独立站?
胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?
中山网站推广排名,中山信息港登录入口?
北京网站制作网页,网站升级改版需要多久?
北京网站制作公司哪家好一点,北京租房网站有哪些?
*请认真填写需求信息,我们会在24小时内与您取得联系。