Search code examples
c#.netwpf

Displaying the time in a WPF Window


I'm creating a WPF app and I want a label called SystemTimeLabel to display the time. The time, as in, the time. I want it to display the present time, not just the time by which it was loaded. (so, like, you can see the digital clock tick.) I think the while statement would do the trick, but I realized that it wouldn't. Any ideas?


Solution

  • Sure - you just need to update the label periodically, which can easily be done with a DispatcherTimer. Just register an event to fire periodically - say 10 times per second - and update the label with the system time every time the event fires.

    (Note that you don't want to do this only once per second, even if you'll only actually change the text once per second, or you could end up with odd effects where it seems to pause for two seconds occasionally due to the timer not firing exactly once per second.)

    It's possible that this could be done with WPF animations instead somehow, but a timer feels like a more natural solution.