我试图找出为什么下面的代码似乎不起作用。它不会给出错误 - 它根本无法扩展。如果我将它更改为我的第二个代码示例,它实际上似乎确实有效。有人知道吗?
谢谢
public static void StartMouseEnterAnimation(Button button)
{
Storyboard storyboard = new Storyboard();
ScaleTransform scale = new ScaleTransform(1.0, 1.0, 1, 1);
button.RenderTransformOrigin = new Point(0.5, 0.5);
button.RenderTransform = scale;
DoubleAnimation growAnimation = new DoubleAnimation();
growAnimation.Duration = TimeSpan.FromMilliseconds(300);
growAnimation.From = 1;
growAnimation.To = 1.8;
storyboard.Children.Add(growAnimation);
Storyboard.SetTargetProperty(growAnimation, new PropertyPath(ScaleTransform.ScaleXProperty));
Storyboard.SetTarget(growAnimation, scale);
storyboard.Begin();
}
--- 以下确实有效,但我必须创建一个 TransformGroup 并通过更复杂的 PropertyChain 引用它...
public static void StartMouseEnterAnimation(Button button)
{
Storyboard storyboard = new Storyboard();
ScaleTransform scale = new ScaleTransform(1.0, 1.0, 1, 1);
button.RenderTransformOrigin = new Point(0.5, 0.5);
TransformGroup myTransGroup = new TransformGroup();
myTransGroup.Children.Add(scale);
button.RenderTransform = myTransGroup;
DoubleAnimation growAnimation = new DoubleAnimation();
growAnimation.Duration = TimeSpan.FromMilliseconds(100);
//growAnimation.From = 1;
growAnimation.To = 1.1;
storyboard.Children.Add(growAnimation);
DependencyProperty[] propertyChain = new DependencyProperty[]
{
Button.RenderTransformProperty,
TransformGroup.ChildrenProperty,
ScaleTransform.ScaleXProperty
};
string thePath = "(0).(1)[0].(2)";
PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain);
Storyboard.SetTargetProperty(growAnimation, myPropertyPath);
Storyboard.SetTarget(growAnimation, button);
storyboard.Begin();
}