<Grid> <Path x:Name="path1" Stroke="Red"> <Path.Data> <EllipseGeometry x:Name="eg" Center="200 120" RadiusX="120" RadiusY="80"></EllipseGeometry> </Path.Data> </Path> <Ellipse Height="33" Stroke="Blue" Fill="Blue" HorizontalAlignment="Left" Margin="0,0,0,0" Name="ellipse1" VerticalAlignment="Top" Width="36"> <Ellipse.RenderTransform> <TransformGroup> <MatrixTransform x:Name="mt1"></MatrixTransform> <TranslateTransform X="-15" Y="-15"></TranslateTransform> </TransformGroup> </Ellipse.RenderTransform> <Ellipse.Triggers> <EventTrigger RoutedEvent="Page.Loaded"> <BeginStoryboard> <Storyboard x:Name="sb"> <MatrixAnimationUsingPath x:Name="ma" Storyboard.TargetName="mt1" Storyboard.TargetProperty="Matrix" Duration="0:0:10"></MatrixAnimationUsingPath> </Storyboard> </BeginStoryboard> </EventTrigger> </Ellipse.Triggers> </Ellipse> <Button Content="开始路径动画" Height="23" HorizontalAlignment="Left" Margin="418,61,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> </Grid>
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { PathGeometry pg = new PathGeometry(); pg.AddGeometry(eg); this.ma.PathGeometry = pg; sb.RepeatBehavior = RepeatBehavior.Forever; sb.Begin(ellipse1); } }