How to have a WPF binding update every second?

后端 未结 2 612
一生所求
一生所求 2020-12-17 22:24

I want to show the user how many seconds have passed since some event occurs. Conceptually, my view model has properties like this:

public DateTime Occurred         


        
2条回答
  •  暖寄归人
    2020-12-17 23:20

    You could create a single DispatcherTimer statically for your view model, and then have all instances of that view model listen to the Tick event.

    public class YourViewModel
    {
        private static readonly DispatcherTimer _timer;
    
        static YourViewModel()
        {
            //create and configure timer here to tick every second
        }
    
        public YourViewModel()
        {
            _timer.Tick += (s, e) => OnPropertyChanged("SecondsSinceOccurence");
        }
    }
    

提交回复
热议问题