WPF中自定义GridLengthAnimation的实例教程


本文摘自PHP中文网,作者零下一度,侵删。

这篇文章主要为大家详细介绍了WPF中自定义GridLengthAnimation的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

需求

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

可以通过将一个Grid,分成两个Cloumn,动态调整两个Cloumn的Width,就可以实现这个需求。

我们知道,Clomun的Width是个,而默认的动画没有这样子的。我们就需要自己实现这样一人动画。

设计
我们从Animation的类图上看到

我们可以从需求

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

可以通过将一个Grid,分成两个Cloumn,动态调整两个Cloumn的Width,就可以实现这个需求。

我们知道,Clomun的Width是个GridLength,而默认的动画没有这样子的。我们就需要自己实现这样一人动画。

设计

我们从Animation的类图上看到AnimationTimeline继承,重写其GetCurrentValue

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

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).通过时间函数的处理,我们使动画过渡不要那么线性。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

/// <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函数。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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);

    }

使用

1

<anim:GridLengthAnimation Storyboard.TargetProperty="Width" From="0" To="*" Duration="0:0:0.5"/>

【相关推荐】

1. ASP.NET免费视频教程

2. C# WinForm中实现快捷键自定义设置实例,_PHP教程

3. 分享微信公众号开发自定义菜单实例教程

以上就是WPF中自定义GridLengthAnimation的实例教程的详细内容!

相关阅读 >>

WPF教程之 xaml基础

WPF教程之 数据更新

WPF教程之 wrappanel控件

WPF教程之 组合框控件

WPF教程之 功能区控件

WPF教程之 带有gridview的listview

WPF教程之 什么是WPF

WPF教程之 windowsformshost控件

visual studio 中自定义生成事件的详细介绍

WPF教程之 treeviews 数据绑定和多种模板

更多相关阅读请进入《WPF》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...