如何使用 GridUnitType.Star 在网格长度上执行动画?



我正在使用以下类在某些网格行高度上执行动画:

internal class GridLengthAnimation : AnimationTimeline
{
static GridLengthAnimation()
{
FromProperty = DependencyProperty.Register("From", typeof(GridLength),
typeof(GridLengthAnimation));
ToProperty = DependencyProperty.Register("To", typeof(GridLength), 
typeof(GridLengthAnimation));
}
public override Type TargetPropertyType 
{
get 
{
return typeof(GridLength);
}
}
protected override System.Windows.Freezable CreateInstanceCore()
{
return new GridLengthAnimation();
}
public static readonly DependencyProperty FromProperty;
public GridLength From
{
get
{
return (GridLength)GetValue(GridLengthAnimation.FromProperty);
}
set
{
SetValue(GridLengthAnimation.FromProperty, value);
}
}
public static readonly DependencyProperty ToProperty;
public GridLength To
{
get
{
return (GridLength)GetValue(GridLengthAnimation.ToProperty);
}
set
{
SetValue(GridLengthAnimation.ToProperty, value);
}
}
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.Pixel);
}
else
return new GridLength(animationClock.CurrentProgress.Value * (toVal - fromVal) + fromVal, GridUnitType.Pixel);
}
}

但是,当尝试将行高动画化为任何带有GridUnitType.Star的任何东西时,这不起作用,因为它会导致动画断断续续,因为快速更改比例单位需要多次重新计算。

一种可能的解决方案是确定行的结果高度(以像素为单位((如果要使用某些 GridUnitType.Star(,然后使用该值启动动画,并在动画完成后将其设置为相应的 GridUnitType.Star。但是,我不确定您将如何确定以像素为单位的行的结果高度。

假设您将行设置为 1*,并让窗口呈现。然后,您可以在行上调用 ActualHeight,并确定当前窗口状态的 1* 以像素为单位,该状态可用于动画。但是有没有办法计算这一点而不必实际将行设置为 1*(出于明显的视觉原因(?

或者,有没有更好的方法来执行支持GridUnitType.Star的网格长度动画?

我有两种替代方法用于这种事情。

两者都依赖于将我的内容放在另一个容器中进行动画处理。这是我动画的高度,父行高度保持不变。

一种方法依赖于一行中的矩形或一些类似的矩形,我用它来获取该行的完整"*"高度。 然后,我将高度从 0 动画化到容器上该矩形的实际高度。

另一种方法。 在子容器上的缩放转换中将 scaleX 和 scaleY 从 0 转换为 1。

最新更新