在code问题应用动画ScaleTransform [英] Applying animated ScaleTransform in code problem
问题描述
我试图找出为什么code以下似乎不工作。它不给一个错误 - 它根本不能扩展。实际上,它似乎如果我改变它作为我的第二个code样品的工作。
任何人有什么想法?
感谢
公共静态无效StartMouseEnterAnimation(Button按钮)
{
故事板分镜=新的故事板(); ScaleTransform规模=新ScaleTransform(1.0,1.0,1,1);
button.RenderTransformOrigin =新的点(0.5,0.5);
button.RenderTransform =规模; DoubleAnimation是growAnimation =新DoubleAnimation是();
growAnimation.Duration = TimeSpan.FromMilliseconds(300);
growAnimation.From = 1;
growAnimation.To = 1.8;
storyboard.Children.Add(growAnimation); Storyboard.SetTargetProperty(growAnimation,新的PropertyPath(ScaleTransform.ScaleXProperty));
Storyboard.SetTarget(growAnimation,规模); storyboard.Begin();
}
---下面的工作,但我必须完成创建的TransformGroup和参考这是一个更复杂的PropertyChain ...
公共静态无效StartMouseEnterAnimation(Button按钮)
{
故事板分镜=新的故事板();
ScaleTransform规模=新ScaleTransform(1.0,1.0,1,1);
button.RenderTransformOrigin =新的点(0.5,0.5);
的TransformGroup myTransGroup =新的TransformGroup();
myTransGroup.Children.Add(规模);
button.RenderTransform = myTransGroup; DoubleAnimation是growAnimation =新DoubleAnimation是();
growAnimation.Duration = TimeSpan.FromMilliseconds(100);
//growAnimation.From = 1;
growAnimation.To = 1.1;
storyboard.Children.Add(growAnimation); 的DependencyProperty [] = propertyChain新的DependencyProperty []
{
Button.RenderTransformProperty,
TransformGroup.ChildrenProperty,
ScaleTransform.ScaleXProperty
};
串thePath =(0)(1)[0](2)。
的PropertyPath myPropertyPath =新的PropertyPath(thePath,propertyChain);
Storyboard.SetTargetProperty(growAnimation,myPropertyPath);
Storyboard.SetTarget(growAnimation,按钮); storyboard.Begin();
}
我能够得到它通过调整你的第一个code样品,像这样的工作:
公共静态无效StartMouseEnterAnimation(Button按钮){
故事板分镜=新的故事板(); ScaleTransform规模=新ScaleTransform(1.0,1.0);
button.RenderTransformOrigin =新的点(0.5,0.5);
button.RenderTransform =规模; DoubleAnimation是growAnimation =新DoubleAnimation是();
growAnimation.Duration = TimeSpan.FromMilliseconds(300);
growAnimation.From = 1;
growAnimation.To = 1.8;
storyboard.Children.Add(growAnimation); Storyboard.SetTargetProperty(growAnimation,新的PropertyPath(RenderTransform.ScaleX));
Storyboard.SetTarget(growAnimation,按钮); storyboard.Begin();
}
而不是新的PropertyPath(ScaleTransform.ScaleXProperty))
,我用新的PropertyPath(RenderTransform.ScaleX))
,和我的故事板的目标设置为按钮(不是scaleTransform本身)。
希望帮助!
I am trying to find out why the code below does not seem to work. It does not give an error - it simply doesn't scale. It actually does seem to work if I change it as to my second code sample. Anyone got any idea?
Thanks
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();
}
--- The following DOES work but I had to create a TransformGroup and reference this through a more complicated 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();
}
I was able to get it to work by tweaking your first code sample like so:
public static void StartMouseEnterAnimation(Button button) {
Storyboard storyboard = new Storyboard();
ScaleTransform scale = new ScaleTransform(1.0, 1.0);
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("RenderTransform.ScaleX"));
Storyboard.SetTarget(growAnimation, button);
storyboard.Begin();
}
Instead of new PropertyPath(ScaleTransform.ScaleXProperty))
, I used new PropertyPath("RenderTransform.ScaleX"))
, and I set the target of the storyboard to the button (not the scaleTransform itself).
Hope that helps!
这篇关于在code问题应用动画ScaleTransform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!