Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4611456
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:16:13+00:00 2026-05-22T01:16:13+00:00

I have a analog clock and a digital clock display. However, I would like

  • 0

I have a analog clock and a digital clock display. However, I would like to use the MVVM pattern. I don’t know where to begin. How to accomplish this? Converting what I have into a pattern shouldn’t be so difficult, should it? Would I need a model class and a view class?

I want to keep this as simple as possible. I am sure that once I get the pattern in I won’t need two timers?

This is what I have so far. I currently have 2 timers one for digital and one for analog. This I know is bad and with the pattern I won’t need it.

public partial class MainWindow : Window
{
    System.Timers.Timer timer = new System.Timers.Timer(1000); //analog clock
    DispatcherTimer timerdigital;                              // digital clock

    public MainWindow() {
        this.InitializeComponent();

        //analog clock
        timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        timer.Enabled = true;

        //digital clock
        timerdigital = new DispatcherTimer();
        timerdigital.Interval = TimeSpan.FromSeconds(1.0);
        timerdigital.Start();
        timerdigital.Tick += new EventHandler(delegate(object s, EventArgs a)
        {
            tbDigital.Text = DateTime.Now.ToString("hh:mm:ss tt");
        });
    }

    //analog clock
    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
        this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
        {
            secondHand.Angle = DateTime.Now.Second * 6;
            minuteHand.Angle = DateTime.Now.Minute * 6; 
            hourHand.Angle = (DateTime.Now.Hour * 30) + (DateTime.Now.Minute * 0.5); 
        }));
    }

For the xaml code, I drew the hands like this

<Rectangle Fill="#FFF21313" Margin="85,28,86,0" Name="rectangleSecond" Opacity="0.8" RadiusX="1" RadiusY="1" RenderTransformOrigin="0.5,1" Stroke="#FFDCDCDC" StrokeDashCap="Round" StrokeEndLineCap="Round" StrokeLineJoin="Round" StrokeStartLineCap="Round" StrokeThickness="1" Height="64" VerticalAlignment="Top" Width="5">
    <Rectangle.RenderTransform>
        <TransformGroup>
            <ScaleTransform ScaleX="1" ScaleY="1" />
            <SkewTransform AngleX="0" AngleY="0" />
            <RotateTransform x:Name="secondHand" Angle="0" />
            <TranslateTransform X="0" Y="0" />
        </TransformGroup>
    </Rectangle.RenderTransform>
</Rectangle>
<Rectangle Fill="#FF181818" Margin="85,27,85,88" Name="rectangleMinute" Opacity="0.8" RadiusX="1" RadiusY="1" RenderTransformOrigin="0.5,1" Stroke="#FFDCDCDC" StrokeDashCap="Round" StrokeEndLineCap="Round" StrokeLineJoin="Round" StrokeStartLineCap="Round" StrokeThickness="1">
    <Rectangle.RenderTransform>
        <TransformGroup>
            <ScaleTransform ScaleX="1" ScaleY="1" />
            <SkewTransform AngleX="0" AngleY="0" />
            <RotateTransform x:Name="minuteHand" Angle="0" />
            <TranslateTransform X="0" Y="0" />
        </TransformGroup>
    </Rectangle.RenderTransform>
</Rectangle>
<Rectangle Fill="#FF070707" HorizontalAlignment="Left" Margin="86,46,0,89" Name="rectangleHour" Opacity="0.8" RadiusX="1" RadiusY="1" RenderTransformOrigin="0.5,1" Stroke="#FFDCDCDC" StrokeDashCap="Round" StrokeEndLineCap="Round" StrokeLineJoin="Round" StrokeStartLineCap="Round" StrokeThickness="1" Width="5">
    <Rectangle.RenderTransform>
        <TransformGroup>
            <ScaleTransform ScaleX="1" ScaleY="1" />
            <SkewTransform AngleX="0" AngleY="0" />
            <RotateTransform x:Name="hourHand" Angle="0" />
            <TranslateTransform X="0" Y="0" />
        </TransformGroup>
    </Rectangle.RenderTransform>
</Rectangle>

I am willing to do this different if I have to.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:16:14+00:00Added an answer on May 22, 2026 at 1:16 am

    I’m not sure why you’re using two different timers, it could be done with the same one.

    All you have to do is put your code in a ViewModel, and bind your view to the properties of the ViewModel.

    ViewModel

    public class ClockViewModel : INotifyPropertyChanged
    {
        private readonly System.Timers.Timer _timer;
    
        public ClockViewModel()
        {
            _timer = new System.Timers.Timer(1000);
            _timer.Elapsed += _timer_Elapsed;
            _timer.Start();
        }
    
        private void _timer_Elapsed(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            DigitalTime = now.ToString("hh:mm:ss tt");
            SecondAngle = now.Second * 6;
            MinuteAngle = now.Minute * 6; 
            HourAngle = (now.Hour * 30) + (now.Minute * 0.5);
        }
    
        private string _digitalTime;
        public string DigitalTime
        {
            get { return _digitalTime;}
            set
            {
                _digitalTime = value;
                OnPropertyChanged("DigitalTime");
            }
        }
    
    
        private double _hourAngle;
        public double HourAngle
        {
            get { return _hourAngle;}
            set
            {
                _hourAngle = value;
                OnPropertyChanged("HourAngle");
            }
        }
    
    
        private double _minuteAngle;
        public double MinuteAngle
        {
            get { return _minuteAngle;}
            set
            {
                _minuteAngle = value;
                OnPropertyChanged("MinuteAngle");
            }
        }
    
        private double _secondAngle;
        public double SecondAngle
        {
            get { return _secondAngle;}
            set
            {
                _secondAngle = value;
                OnPropertyChanged("SecondAngle");
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    XAML

    <Rectangle Fill="#FFF21313" Margin="85,28,86,0" Name="rectangleSecond" Opacity="0.8" RadiusX="1" RadiusY="1" RenderTransformOrigin="0.5,1" Stroke="#FFDCDCDC" StrokeDashCap="Round" StrokeEndLineCap="Round" StrokeLineJoin="Round" StrokeStartLineCap="Round" StrokeThickness="1" Height="64" VerticalAlignment="Top" Width="5">
        <Rectangle.RenderTransform>
            <RotateTransform Angle="{Binding SecondAngle}" />
        </Rectangle.RenderTransform>
    </Rectangle>
    <Rectangle Fill="#FF181818" Margin="85,27,85,88" Name="rectangleMinute" Opacity="0.8" RadiusX="1" RadiusY="1" RenderTransformOrigin="0.5,1" Stroke="#FFDCDCDC" StrokeDashCap="Round" StrokeEndLineCap="Round" StrokeLineJoin="Round" StrokeStartLineCap="Round" StrokeThickness="1">
        <Rectangle.RenderTransform>
            <RotateTransform Angle="{Binding MinuteAngle}" />
        </Rectangle.RenderTransform>
    </Rectangle>
    <Rectangle Fill="#FF070707" HorizontalAlignment="Left" Margin="86,46,0,89" Name="rectangleHour" Opacity="0.8" RadiusX="1" RadiusY="1" RenderTransformOrigin="0.5,1" Stroke="#FFDCDCDC" StrokeDashCap="Round" StrokeEndLineCap="Round" StrokeLineJoin="Round" StrokeStartLineCap="Round" StrokeThickness="1" Width="5">
        <Rectangle.RenderTransform>
            <RotateTransform Angle="{Binding HourAngle}" />
        </Rectangle.RenderTransform>
    </Rectangle>
    

    (I removed the TransformGroups since only the RotateTransforms were used)

    In the code-behind, just assign an instance of ClockViewModel to the DataContext.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a windows form analog clock and I need to create a web
I have a MVC class where i am inheriting INotifyPropertyChanged I have a clock
Does msvc have analog of gcc's ({ }). I assume the answer is no.
I'm redesigning an analog gauge widget I made a while back, essentially wanting to
i have a AnalogClock in layout. if i touch the AnalogClock the next activity
I posted this question previously and now have the localized strings loaded (the ones
So, in example of DotNetOpenAuth I have form in aspx: <form action=Authenticate?ReturnUrl=<%=HttpUtility.UrlEncode(Request.QueryString[ReturnUrl]) %> method=post
I've got a c homework problem that is doing my head in and will
Part of the application we are developing is about to be provided to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.