今天, 我给大家讲解如何用C#动态改变grid容器的行宽或者列高.WPF的Storyboard真的很强大, 尤其是和blend 3配和后就更容易了, 但是呢仔细看下Grid的width/height的数据类型是GridLength, 也就是说不是普通的Int, Double, Boolean
, Char
, Byte
, Color
, Point
类型, 而wpf也没有给我们提供一个GridLength的动画类.GridLength是一个struct.
(一) 创建一个支持GridLength类型的动画类
我们新建一个继承AnimationTimeLine的类GridLengthAnimation, 我们简单实现2个依赖属性"From", "To".代码如何:
internal class GridLengthAnimation : AnimationTimeline { static GridLengthAnimation() { FromProperty = DependencyProperty.Register("From", typeof(GridLength), typeof(GridLengthAnimation)); ToProperty = DependencyProperty.Register("To", typeof(GridLength), typeof(GridLengthAnimation)); } public static readonly DependencyProperty FromProperty; public GridLength From { get { return (GridLength)GetValue(GridLengthAnimation.From