I have created the following XAML
<Canvas Background="Gray" Margin="10">
<Ellipse x:Name="Node1" Width="20" Height="20" Fill="AliceBlue" Canvas.Left="38" Canvas.Top="136" />
<Ellipse x:Name="Node2" Width="20" Height="20" Fill="AliceBlue" Canvas.Left="290" Canvas.Top="136" />
<Ellipse x:Name="object" Width="10" Height="20" Fill="Black" Canvas.Left="43" Canvas.Top="125" />
<Path Stroke="Black" StrokeThickness="1">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigureCollection>
<PathFigure StartPoint="50,145">
<PathFigure.Segments>
<PathSegmentCollection>
<LineSegment Point="100,100" />
<LineSegment Point="250,100" />
<LineSegment Point="300,145" />
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
</PathFigureCollection>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
</Canvas>
As you can see, I have created 2 elliptical Nodes. A path connecting the two nodes and an object sitting at node 1. All I want to do here is to animate the object at node1 along the path towards node2.
I am trying to do the animation using code as I want the animation to happen on click of the node2. I have been struggling with DoubleAnimation, MatrixAnimation,storyboard..Very confusing. Please share you knowledge on how to achieve this. I am hoping the same code would work with curves and complex paths.
You need DoubleAnimationUsingPath
(ref):
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingPath Duration="0:0:2" Source="X" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="object">
<DoubleAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M2,10 L52,-35 L202,-35 L252,10"/>
</DoubleAnimationUsingPath.PathGeometry>
</DoubleAnimationUsingPath>
<DoubleAnimationUsingPath Duration="0:0:2" Source="Y" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="object">
<DoubleAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M2,10 L52,-35 L202,-35 L252,10"/>
</DoubleAnimationUsingPath.PathGeometry>
</DoubleAnimationUsingPath>
</Storyboard>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<Canvas Background="Gray" Margin="10">
<Ellipse x:Name="Node1" Width="20" Height="20" Fill="AliceBlue" Canvas.Left="38" Canvas.Top="136" />
<Ellipse x:Name="Node2" Width="20" Height="20" Fill="AliceBlue" Canvas.Left="290" Canvas.Top="136" />
<Ellipse x:Name="object" Width="10" Height="20" Fill="Black" Canvas.Left="43" Canvas.Top="125" RenderTransformOrigin="0.5,0.5" MouseLeftButtonDown="object_MouseLeftButtonDown" >
<Ellipse.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Ellipse.RenderTransform>
</Ellipse>
<Path Stroke="Black" StrokeThickness="1">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigureCollection>
<PathFigure StartPoint="50,145">
<PathFigure.Segments>
<PathSegmentCollection>
<LineSegment Point="100,100" />
<LineSegment Point="250,100" />
<LineSegment Point="300,145" />
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
</PathFigureCollection>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
</Canvas>
</Grid>
</Window>
Then to invoke from code:
private void object_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Storyboard animation = this.TryFindResource("Storyboard1") as Storyboard;
animation.Begin();
}
[EDIT] I think you're missing critical tool here: Blend. As for creating animation in code, if you look at XAML elements, think about them as serialized classes, which can be reflected in code, i.e.
Storyboard sb = new Storyboard();
DoubleAnimationUsingPath ani_2 = new DoubleAnimationUsingPath();
ani_2.Duration = new Duration(new TimeSpan(0, 0, 2));
PathGeometry pg = new PathGeometry();
pg.Figures.Add(new PathFigure());
ani_2.PathGeometry.AddGeometry(pg);
etc. It is however (IMAO) fairly painful to create those straight from code. It all really depends on the application. Have a look here for a starting point on Blend.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With