WPF中自定义GridLengthAnimation

 更新时间:2017年05月20日 16:10:38   作者:HelyCheng  
这篇文章主要为大家详细介绍了WPF中自定义GridLengthAnimation的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

需求

我们想在编辑一个列表中某一个条目时,将编辑的详情内容也放置当前面,比如右侧。

可以通过将一个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"/>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Asp.net webForm设置允许表单提交Html的方法

    Asp.net webForm设置允许表单提交Html的方法

    下面小编就为大家分享一篇Asp.net webForm设置允许表单提交Html的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • 分享一个取自HoverTree项目的.NET分页类

    分享一个取自HoverTree项目的.NET分页类

    分页显示是一种非常常见的浏览和显示大量数据的方法,属于web编程中最常处理的事件之一。对于web编程的老手来说,编写这种代码实在是和呼吸一样自然,但是对于初学者来说,常常对这个问题摸不着头绪,今天我们给大家分享一个取自HoverTree项目的.NET分页类。
    2015-04-04
  • web用户控件调用.aspx页面里的方法

    web用户控件调用.aspx页面里的方法

    今天在一QQ技术群有朋友问: 他在web用户控件中(.ascx)中放了一个dropdownlist控件,一个textbox控件和一个button控件。
    2009-04-04
  • asp.net 防止SQL注入攻击

    asp.net 防止SQL注入攻击

    asp.net网站防止SQL注入攻击,通常的办法是每个文件都修改加入过滤代码,这样很麻烦,下面介绍一种办法,可以从整个网站防止注入。
    2009-06-06
  • asp.net 细说文件读写操作(读写锁)

    asp.net 细说文件读写操作(读写锁)

    开发过程中,我们玩玩需要大量与文件交互,读文件,写文件已成家常便饭,本地运行完美,但一上到投产环境,往往会出现很多令人措手不及的意外,或开发中的烦恼,因此,我对普通的C#文件操作做了一次总结
    2011-12-12
  • asp.net动态产生checkbox(数据源为DB或内存集合)

    asp.net动态产生checkbox(数据源为DB或内存集合)

    动态产生一组checkbox(数据源为DB或内存集合)且post提交时后台能及时获取等等,打算使用repeater+input(checkbox)+input(hidden)来实现
    2013-10-10
  • ASP.NET性能优化小结(ASP.NET&C#)

    ASP.NET性能优化小结(ASP.NET&C#)

    ASP.NET性能优化,提高页面的执行效率与下载速度,等很多需要考虑的细节,编程人员值得参考下。
    2011-01-01
  • Visual Studio调试技巧汇总

    Visual Studio调试技巧汇总

    这篇文章总结了可能节省你大量时间的11个visual studio的调试技巧和方法,感兴趣的小伙伴们可以参考一下
    2015-11-11
  • ASP.NET2.0缓存(Cache)技术深入理解

    ASP.NET2.0缓存(Cache)技术深入理解

    缓存技术是ASP.NET2.0非常重要的一个特性,它提供了一种非常好的本地数据缓存机制,从而有效的提高数据访问的性能
    2012-11-11
  • gridview实现服务器端和客户端全选的两种方法分享

    gridview实现服务器端和客户端全选的两种方法分享

    这篇文章主要介绍了gridview实现服务器端和客户端全选的两种方法,需要的朋友可以参考下
    2014-02-02

最新评论