How to animate Margin property in WPF

前端 未结 5 727
攒了一身酷
攒了一身酷 2020-12-13 09:23

I want to move animate an rectangle object to move it in x-axis. I am new to WPF animation, started out with the following:



        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 10:02

    You can't animate Margin.Left (because Left is not a dependency property), but you can animate Margin. Use ObjectAnimationUsingKeyFrames:

    
        
            
                
                    134,70,0,0
                
            
            
                
                    50,70,0,0
                
            
        
    
    

    There are some alternatives that allow you to use a DoubleAnimation, rather than key frames:

    1. Place your target inside a Canvas, and animate its x position using Canvas.Left.
    2. Apply a TranslateTransform to your target, and animate its x position using TranslateTransform.X.

提交回复
热议问题