Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I only have experience with C/C++ and just moved to C# and WPF. I want to create an animation to move a component(e.g an image), but I don't know why the following is illegal:

ThicknessAnimation a = new ThicknessAnimation(...);

Image1.BeginAnimation(Image1.Margin, a); // illegal. Image.Margin illegal too

It seems just can't use Margin here. Of course create a timer and create Thickness objects for Margin manually can work, but that will be dull and if Animation is possible, it will be more elegant.

Is a storyboard required here? I heard some says create a storyboard and you can use Margin property, but I don't know storyboard at all and can't understand that. Thanks

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You do animate dependency properties, not regular ones so try Image.MarginProperty instead.

These are static fields inside the type or base type you are targeting.

public static readonly DependencyProperty MarginProperty

In your case, it is defined in FrameworkElement.

References :

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.marginproperty.aspx

http://msdn.microsoft.com/en-us/library/ms590761.aspx

For more complex animations you would need a storyboard :

Storyboard objects enable you to combine timelines that affect a variety of objects and properties into a single timeline tree, making it easy to organize and control complex timing behaviors.

http://msdn.microsoft.com/en-us/library/ms742868.aspx

EDIT :

When looking at Intellisense you can see that the associated dependency property sits below the conventional property.

enter image description here

I previously mentionned that you had to use Image.MarginProperty but in fact you can just ignore the 'Image.' part as the object does inherit from that base type already, like animating 'this' :

enter image description here

share|improve this answer
    
Thanks, but how can I find the correct name of property(MarginProperty here) without looking up in the MSDN large class tree document? Is there a simpler table to lookup? –  Cherry Jun 14 '13 at 7:00
    
Their name is the original property name with 'Property' appended at the end. See Dependency Property Name Conventions : msdn.microsoft.com/en-us/library/ms753358.aspx#checklist –  Aybe Jun 14 '13 at 11:22
    
I've added a few more explanations to my answer. –  Aybe Jun 14 '13 at 12:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.