简体   繁体   中英

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?

If you want to follow the MVVM pattern, the main problem with a datetime is that it doesn't implement INotifyPropertyChanged. So while you can bind a datetime property to your label, it won't update automatically. So, you just need to implement update notifications for your datetime property in your viewmodel. So, create a public property with change notification, and start a timer to update that property every half second or so. Then bind your label's content to that property.

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private DateTime _now;

    public MyViewModel()
    {
        _now = DateTime.Now;
        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromMilliseconds(500);
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();
    }

    public DateTime CurrentDateTime
    {
        get {return _now;}
        private set
        {
            _now = value;
            PropertyChanged(this, new PropertyChangedEventArgs("CurrentDateTime"));
        }
    }

    void timer_Tick(object sender, EventArgs e)
    {
        CurrentDateTime = DateTime.Now;
    }

}

<Label x:Name="SomeLabel" Content="{Binding CurrentDateTime}" ContentStringFormat="yyyy-MM-dd HH:mm:ss" />

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.

You can subscribe to the Timer Elapsed Event, which will then change the value of your property and that will trigger the NotificationChangedEvent. And Bind CurrentTime to your element in View.

    private DateTime _currentTime;
    private Timer _timer = new Timer(1000);

    public DateTime CurrentTime
    {
        get { return _currentTime; }
        set { 
        if (object.Equals(_currentTime , value)) return;
        _currentTime = value;
        PropertyChanged(this, new PropertyChangedEventArgs(_currentTime)) }
    }
    public MainWindowViewModel()
    {
        CurrentTime = DateTime.Now;
        _timer.Elapsed += (s, e) => CurrentTime = DateTime.Now.ToLocalTime();
        _timer.Start();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM