Sliding Clock Animation didn't Work

IKINGSHADOW 136 Reputation points

hello everyone , I try to Make A clock Like Android Phones Sliding Clocks in Wpf But I dont know why my codes didn't work correctly ,
could you help me please :



because there was so many codes i upload the project in mega , so you can download it from link below:

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,626 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,056 Reputation points

    you cam simplified your code:

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media.Animation;
    using System.Windows.Threading;
    namespace Sliding_Watch
      /// <summary>
      /// Interaction logic for MainWindow.xaml
      /// </summary>
      public partial class MainWindow : Window
        double[] digits = new double[6];
        public MainWindow()
          DispatcherTimer MyTimer = new DispatcherTimer();
          MyTimer.Interval = TimeSpan.FromSeconds(1);
          MyTimer.Tick += MyTimer_Tick;
        private void ClockAnimation(StackPanel MyPanel, double MyTo)
          Storyboard MyStory = new Storyboard();
          ThicknessAnimation ani = new ThicknessAnimation();
          ani.From = MyPanel.Margin;
          ani.To = new Thickness(0, MyTo, 0, 0);
          ani.Duration = new Duration(TimeSpan.FromMilliseconds(100));
          Storyboard.SetTarget(ani, MyPanel);
          Storyboard.SetTargetProperty(ani, new PropertyPath("Margin"));
        private void MyTimer_Tick(object sender, EventArgs e)
          double[] newdigits = GetDigits();
          if (digits[0] != newdigits[0]) ClockAnimation(StackHours01, -newdigits[0] * 150);
          if (digits[1] != newdigits[1]) ClockAnimation(StackHours02, -newdigits[1] * 150);
          if (digits[2] != newdigits[2]) ClockAnimation(StackMins01, -newdigits[2] * 150);
          if (digits[3] != newdigits[3]) ClockAnimation(StackMins02, -newdigits[3] * 150);
          if (digits[4] != newdigits[4]) ClockAnimation(StackSec01, -newdigits[4] * 150);
          if (digits[5] != newdigits[5]) ClockAnimation(StackSec02, -newdigits[5] * 150);
          digits = newdigits;
        private double[] GetDigits()
          double[] d = new double[6];
          string t = DateTime.Now.ToString("HHmmss");
          for (int i = 0; i < 6; i++) d[i] = int.Parse(t.Substring(i, 1));
          return d;
        private void MainBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. DaisyTian-1203 11,611 Reputation points

    How about the below workaround without using Animation?
    1.Put the

     double MyHours = DateTime.Now.Hour;  
     double MyMinute = DateTime.Now.Minute;  
     double MySecond = DateTime.Now.Second;  

    into MyTimer_Tick, otherwise values obtained will not change
    2. Add below code to replace the code in ClockAnimation

                Thickness thick = new Thickness(0, MyTo, 0, 0);  
                MyPanel.Margin = thick;  

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments