IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

Animation Grid couleur wpf


Sujet :

Windows Presentation Foundation

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2014
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2014
    Messages : 11
    Points : 6
    Points
    6
    Par défaut Animation Grid couleur wpf
    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

  2. #2
    Membre expert
    Avatar de Pragmateek
    Homme Profil pro
    Formateur expert .Net/C#
    Inscrit en
    Mars 2006
    Messages
    2 635
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Formateur expert .Net/C#
    Secteur : Conseil

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 635
    Points : 3 958
    Points
    3 958
    Par défaut
    De la même façon tu peux désigner le Rectangle :

    Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Storyboard.SetTarget(anim1, MyRectangle);
    Storyboard.SetTargetProperty(anim1, new PropertyPath("Fill.Color"));
    Formateur expert .Net/C#/WPF/EF Certifié MCP disponible sur Paris, province et pays limitrophes (enseignement en français uniquement).
    Mon blog : pragmateek.com

Discussions similaires

  1. [Débutant] Animation bouton c# wpf
    Par yrtera dans le forum Windows Presentation Foundation
    Réponses: 4
    Dernier message: 28/03/2012, 16h50
  2. Animer la couleur d'un radial
    Par LeBauw dans le forum Flash
    Réponses: 1
    Dernier message: 28/10/2011, 00h47
  3. Palette de Couleur [WPF]
    Par 3logy dans le forum Windows Presentation Foundation
    Réponses: 10
    Dernier message: 09/04/2010, 11h34
  4. grid dans wpf
    Par asprog dans le forum VB.NET
    Réponses: 0
    Dernier message: 05/12/2008, 12h39
  5. String Grid et choix d'une couleur pour une ligne
    Par Gigottine dans le forum C++Builder
    Réponses: 12
    Dernier message: 17/05/2002, 15h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo