I have attached a sample project, which shows one approach for achieving this functionality.
In the attached application, the countdown is represented by a property of the type TimeSpan, whose value is updated each second by a DispatcherTimer class instance.
The following code snippets show this approach's implementation:
publicclassItemModel : ViewModelBase
{
private TimeSpan countdown;
public TimeSpan Countdown
{
get { returnthis.countdown; }
set { this.countdown = value; this.OnPropertyChanged(nameof(this.Countdown)); }
}
}
publicclassMainViewModel
{
private DispatcherTimer dispatcherTimer;
publicMainViewModel()
{
this.ItemModels = new ObservableCollection<ItemModel>()
{
new ItemModel()
{
Countdown = new TimeSpan(0, 0, 60),
}
};
this.ItemModel = this.ItemModels[0];
this.dispatcherTimer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, OnIntervalElapsed, Application.Current.Dispatcher);
this.dispatcherTimer.Stop();
}
public ItemModel ItemModel { get; set; }
public ObservableCollection<ItemModel> ItemModels { get; set; }
privatevoidOnIntervalElapsed(object sender, EventArgs e)
{
this.ItemModel.Countdown = this.ItemModel.Countdown.Add(TimeSpan.FromSeconds(-1));
if (this.ItemModel.Countdown <= TimeSpan.Zero)
{
this.dispatcherTimer.Stop();
}
}
}
The produced result is as follows:
Could you give the sample project a try?
Regards,
Stenly
Progress Telerik
The Premier Dev Conference is back!
Coming to you live from Progress360 in-person or on your own time, DevReach for all. Register Today.