开发者

Not being able to animate TranslateTransform.XProperty with code

I have a method that enables me to animate objects that have to do with a DoubleAnimation:

public void animDouble(DependencyObject target, DependencyProperty property, double to, TimeSpan duration, double? from = null, TimeSpan? beginTime = null, IEasingFunction e = null)
{

    DoubleAnimation animation = new DoubleAnimation();
    animation.To = to;

    if (beginTime == null)
        beginTime = TimeSpan.FromSeconds(0);

    if (from != null)
        animation.From = from;


    animati开发者_StackOverflow中文版on.BeginTime = beginTime;
    animation.Duration = duration;


    if (e != null)
        animation.EasingFunction = e;

    //start animating
    Storyboard.SetTarget(animation, target);  // what object will be animated?
    Storyboard.SetTargetProperty(animation, new PropertyPath(property)); // what property will be animated
    Storyboard sb = new Storyboard();
    sb.Children.Add(animation);
    sb.Begin();
}

so if I have a boarder called br1 for example and I want to animate it's height I will call the method as:

animDouble(br1, FrameworkElement.HeightProperty, 150, TimeSpan.FromSeconds(5));

if I want to animate it's width I will do:

animDouble(br1, FrameworkElement.WidthProperty, 150, TimeSpan.FromSeconds(5));

I could also animate it's visibility with the same method.

for some reason I am not able to animate its x property in order to translate it along the x axis or y-axis. When I call the method as:

a.animDouble(br1, TranslateTransform.XProperty, 150, TimeSpan.FromSeconds(5));

the boarder does not animates. I don't get any errors aether.


Somehow i would have expected an error, well, anyway, the Border owns no such property, if you want to move your control you need to set the RenderTransform or LayoutTransform of the border to a TranslateTransform, then you can pass the transform itself into the method as target.

(The whole storyboard is awfully redundant as you only have one animation, you can just call BeginAnimation on the target itself)


It had to do with registering the name. I found a link in here

I don't know what the method registerName does but I guess I needed it. from the page I managed to get the basic animations. I where not able to animate two things at once SOMETIMES. if you are interested in seeing the method take a look tat this question. I think it is a prety nice class that will enable to create animations with code. copy the namespace to visual studio and copy the first example that I posted so that you can see how it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜