1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
public sealed class CustomAnimatedPath : Shape
{
#region Dependency Properties
public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(Geometry), typeof(CustomAnimatedPath), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
#endregion
#region CLR Properties
[TypeConverter(typeof(GeometryConverter))]
public Geometry Data
{
get { return (Geometry)base.GetValue(DataProperty); }
set { base.SetValue(DataProperty, value); }
}
#endregion
#region Overrides
protected override Geometry DefiningGeometry
{ get { return this.Data ?? Geometry.Empty; } }
#endregion
private Ellipse ellipse = new Ellipse();
private PathGeometry chemin=null;
private MatrixTransform ellipseMatrixTransform_LtR = new MatrixTransform();
private MatrixAnimationUsingPath ellipseMatrixAnimation_LtR = new MatrixAnimationUsingPath();
private Storyboard pathAnimationStoryboard_LtR = new Storyboard();
private static bool _isStartedLeftToRight = false;
public void StartLeftToRight()
{
if (!_isStartedLeftToRight)
{
NameScope.SetNameScope(getFenetreMere(), new NameScope());
ellipse.Stroke = Brushes.Black;
ellipse.Fill = Brushes.AliceBlue;
ellipse.StrokeThickness = 1;
ellipse.Width = 10;
ellipse.Height = 10;
ellipse.Visibility = Visibility.Visible;
ellipse.RenderTransform = ellipseMatrixTransform_LtR;
if (!(getCanvasPere().Children.Contains(ellipse)))
getCanvasPere().Children.Add(ellipse);
// enregistre
string nom = "ellipse_" + base.Name + "_MatrixTransform_LtR";
if (nom == null)
nom = "ellipse_" + base.Name + "_MatrixTransform_LtR";
if (getFenetreMere().FindName(nom) == null)
getFenetreMere().RegisterName(nom, ellipseMatrixTransform_LtR);
if (chemin == null)
chemin = PathGeometry.CreateFromGeometry(Data);
// empeche la modification du chemin
chemin.Freeze();
// definit les caraceristiques de l'aimation
ellipseMatrixAnimation_LtR.PathGeometry = chemin;
ellipseMatrixAnimation_LtR.Duration = TimeSpan.FromSeconds(0.5);
ellipseMatrixAnimation_LtR.RepeatBehavior = RepeatBehavior.Forever;
// impose l animation a l ellipse
Storyboard.SetTargetName(ellipseMatrixAnimation_LtR, nom);
Storyboard.SetTargetProperty(ellipseMatrixAnimation_LtR, new PropertyPath(MatrixTransform.MatrixProperty));
// cree la Storyboard pour contenier et appliquer l animation
pathAnimationStoryboard_LtR.Children.Clear();
pathAnimationStoryboard_LtR.Children.Add(ellipseMatrixAnimation_LtR);
//commence l animation
pathAnimationStoryboard_LtR.Begin(getFenetreMere(), true);
}
}
private Canvas getCanvasPere()
{
return (Canvas)(VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(this)));
}
private Window getFenetreMere()
{
return (Window)VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(this)))))));
}
} |