Bonjour ,

Je débute dans le WPF et dans l'application que j'essaie de créer, j'ai besoin de réaliser une animation, ici une transition de couleur sur le (la?) grid de l'application.

C#
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
 
namespace WpfApplication1
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int time = 0;
        private DispatcherTimer Timer;
        public MainWindow()
        {
            InitializeComponent();
            Timer = new DispatcherTimer();
            Timer.Interval = TimeSpan.FromSeconds(1);
            Timer.Tick += Timer_Tick;
 
        }
        void Timer_Tick(object sender, EventArgs e)
        {
            if (time > 0)
            {
                string zeroHeure = "";
                string zeroMinutes = "";
                string zeroSecondes = "";
                int heure = time / 3600;
                int minutes = (time - 3600 * (time / 3600)) / 60;
                int secondes = time % 60;
                if (heure<10)
                {
                    zeroHeure = "0";
                }
                if (minutes < 10)
                {
                    zeroMinutes = "0";
                }
                if (secondes < 10)
                {
                    zeroSecondes = "0";
                }
                lblTime.Text = string.Format("{0}:{1}:{2}", zeroHeure + heure.ToString(), zeroMinutes + minutes.ToString(), zeroSecondes + secondes.ToString());
                time--;
            }
            else
            {
                lblTime.Text = string.Format("00:00:00");
                Timer.Stop();
            }
        }
 
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //Grille.Background = Brushes.Blue;
            ColorAnimation anim1 = new ColorAnimation(Colors.Green, Colors.Yellow, new Duration(TimeSpan.FromSeconds(4)));
            Storyboard.SetTarget(anim1, this);
            Storyboard.SetTargetProperty(anim1, new PropertyPath("Background.Color"));
            Storyboard storyboard1 = new Storyboard();
            storyboard1.Children.Add(anim1);
            storyboard1.Begin();
            time = 50;
            Timer.Start();
        }
    }
}
XAML
Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<Window x:Class="WpfApplication1.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">
    <Grid x:Name="Grille">
        <TextBlock Name="lblTime" FontSize="56" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black" Width="300" TextAlignment="Center" FontWeight="Bold">
        </TextBlock>
        <Button Name="essai" Content="Button" HorizontalAlignment="Left" Margin="10,288,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
        <Rectangle
        Name="MyRectangle"
        Width="100" 
        Height="100"
        Fill="Blue">
        </Rectangle>
    </Grid>
</Window>
Ce code fonctionne parfaitement bien, où est le problème me direz-vous.
Le problème c'est que j'ai réussi à cibler un peu au hasard sur le grid en mettant this à "SetTarget", mais évidemment le hasard c'est pas genial
Code : Sélectionner tout - Visualiser dans une fenêtre à part
Storyboard.SetTarget(anim1, this);
Quel chemin dois-je mettre à la place de this si je veux atteindre "MyRectangle" (créé ici pour l'exemple) et que l'animation se réalise. J'en ai essayé plusieurs mais l'application plante. (Et plus généralement quelqu'un aurait-il un tuyau pour cibler facilement les controles sans se planter ?)

Merci beaucoup