0

I have an ObservableCollection of DateTime bind to a listbox. It shows current Time for various countries. Listbox has an itemtemplate so that i can format how the HH:MM:SS are displayed.

Now, I need to update the time for each item in ObservableCollection of DateTime every 1 second, so that the UI is also updated accordingly.

What is the best way to achieve this ?

NoobDeveloper
  • 1,877
  • 6
  • 30
  • 55
  • 1
    I always prefer delegate over DispatcherTimer to do these kind of things, which is more light weight. Async calls using delegate could update the UI thread through current despatcher. – Vimal CK Sep 07 '13 at 17:45

1 Answers1

2

Most advanced (and simplest) way is to use Rx extension:

Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1)).
Subscribe(o =>
{
   // every second set current time on every item. 
   foreach(var item in YourCollection)
   {
    item.CurrentTime = DateTime.Now;
   }
});

and of course in item.CurrentTime setter you need to raise propertychange event.

Andrej B.
  • 170
  • 6
  • 1
    Might want to include `ObserveOnDispatcher` to make sure the subscription executes on the UI thread. Or make sure the PropertyChange event is fired on the UI thread. – Cameron MacFarland Sep 09 '13 at 09:13