WPF. Easiest way to move Image to (X,Y) programmatically?

前端 未结 5 1499
谎友^
谎友^ 2020-11-30 12:52

Does anyone know of an easy way to animate a movement from an Image\'s current location to a new location (X,Y) using WPF animation with no XAML, 100% programmatically? And

5条回答
  •  时光取名叫无心
    2020-11-30 13:10

    Please find a solution that uses the Left and Top properties of Canvas for the extension method. See the following code:

        public static void MoveTo(this Image target, Point newP)
        {
    
            Point oldP = new Point();
            oldP.X = Canvas.GetLeft(target);
            oldP.Y = Canvas.GetTop(target);
    
            DoubleAnimation anim1 = new DoubleAnimation(oldP.X, newP.X, TimeSpan.FromSeconds(0.2));
            DoubleAnimation anim2 = new DoubleAnimation(oldP.Y, newP.Y , TimeSpan.FromSeconds(0.2));
    
            target.BeginAnimation(Canvas.LeftProperty , anim1);
            target.BeginAnimation(Canvas.TopProperty, anim2);
    
        }
    

提交回复
热议问题