[Html]
<Window x: Class = "Wpfdemo1.MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "350" Width = "525" Loaded = "Window_Loaded" MouseLeftButtonDown = "Window_MouseLeftButtonDown">
<Canvas x: Name = "body">
</Canvas>
</Window>
[Csharp]
/// <Summary>
/// Interaction logic of MainWindow. xaml
/// </Summary>
Public partial class MainWindow: Window
{
Ellipse ell;
Public MainWindow ()
{
InitializeComponent ();
Ell = new Ellipse ();
Ell. Fill = new SolidColorBrush (Colors. Red );
Ell. Width = 50;
Ell. Height = 50;
Body. Children. Add (ell );
Canvas. SetLeft (well, 100 );
Canvas. SetTop (well, 100 );
}
Private void Window_Loaded (object sender, RoutedEventArgs e)
{
}
Private void Window_MouseLeftButtonDown (object sender, MouseButtonEventArgs e)
{
MoveTo (e. GetPosition (body ));
}
Private void moveTo (Point region Point)
{
// Point p = e. GetPosition (body );
Point curPoint = new Point ();
CurPoint. X = Canvas. GetLeft (ell );
CurPoint. Y = Canvas. GetTop (ell );
Double _ s = System. math. sqrt (Math. pow (vertex point. x-curPoint. x), 2) + Math. pow (vertex point. y-curPoint. y), 2 ));
Double _ secNumber = (_ s/1000) * 500;
Storyboard storyboard = new Storyboard ();
// Create an animation in the X axis direction
DoubleAnimation doubleAnimation = new DoubleAnimation (
Canvas. GetLeft (ell ),
Vertex point. X,
New Duration (TimeSpan. FromMilliseconds (_ secNumber ))
);
Storyboard. SetTarget (doubleAnimation, ell );
Storyboard. SetTargetProperty (doubleAnimation, new PropertyPath ("(Canvas. Left )"));
Storyboard. Children. Add (doubleAnimation );
// Create an animation in the Y axis
DoubleAnimation = new DoubleAnimation (
Canvas. GetTop (ell ),
Vertex point. Y,
New Duration (TimeSpan. FromMilliseconds (_ secNumber ))
);
Storyboard. SetTarget (doubleAnimation, ell );
Storyboard. SetTargetProperty (doubleAnimation, new PropertyPath ("(Canvas. Top )"));
Storyboard. Children. Add (doubleAnimation );
// Play the animation
Storyboard. Begin ();
}
}
The storyboard animation function allows you to move elements.