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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.Animation;
namespace WpfAnimationVM
{
public class MainVM : DependencyObject
{
private Storyboard XTranslate,YTranslate;
private DoubleAnimation XAnimation, YAnimation;
public MainVM()
{
XTranslate = Application.Current.Resources["XTranslateStoryboard"] as Storyboard;
XAnimation = XTranslate.Children[0] as DoubleAnimation;
Storyboard.SetTarget(XAnimation, this);
RightTranslateCommand = new RelayCommand(RightTranslateExecute);
LeftTranslateCommand = new RelayCommand(LeftTranslateExecute);
YTranslate = Application.Current.Resources["YTranslateStoryboard"] as Storyboard;
YAnimation = YTranslate.Children[0] as DoubleAnimation;
Storyboard.SetTarget(YAnimation, this);
UpTranslateCommand = new RelayCommand(UpTranslateExecute);
DownTranslateCommand = new RelayCommand(DownTranslateExecute);
}
// les DP TranslateX & TranslateY sont les coords X,Y de l'objet
// ainsi denommées car "bindées" aux DP du TranslateTransform
public double TranslateX
{
get { return (double)GetValue(TranslateXProperty); }
set { SetValue(TranslateXProperty, value); }
}
public static readonly DependencyProperty TranslateXProperty =
DependencyProperty.Register("TranslateX",
typeof(double),
typeof(MainVM),
new PropertyMetadata(0D)
);
public double TranslateY
{
get { return (double)GetValue(TranslateYProperty); }
set { SetValue(TranslateYProperty, value); }
}
public static readonly DependencyProperty TranslateYProperty =
DependencyProperty.Register("TranslateY",
typeof(double),
typeof(MainVM),
new PropertyMetadata(0D)
);
// partie commandes
public RelayCommand RightTranslateCommand { get; set; }
public RelayCommand LeftTranslateCommand { get; set; }
public RelayCommand UpTranslateCommand { get; set; }
public RelayCommand DownTranslateCommand { get; set; }
private void LeftTranslateExecute(object o)
{
XAnimation.By = -50;
XTranslate.Begin();
}
private void RightTranslateExecute(object o)
{
XAnimation.By = 50;
XTranslate.Begin();
}
private void UpTranslateExecute(object o)
{
YAnimation.By = -50;
YTranslate.Begin();
}
private void DownTranslateExecute(object o)
{
YAnimation.By = 50;
YTranslate.Begin();
}
}
} |
Partager