tags:

views:

212

answers:

1

I'm using the MVVM pattern and I have a POCO (in my Model) with a Start Date property.

I want to show the elapsed time since the start date in a control on a WPF window/user control...

I don't see how I can bind a ModelView property to a UI control and have it update this duration automatically...can anyone suggest a way?

I could use something (a timer or a thread) to update a duration property on my ModelView but I just don't see any other way because as I understand it the UI will only update when a property value changes. However the start date on my POCO isn't changing it's just the elapsed time that's changing which is a calculated value.

Am I missing something?

A: 

You're on the right track. Have a look at the Presentation Model pattern on Martin Fowler's page.

The basic idea is to build a model for the UI (ViewModel) and have the UI just sync up with it. Every bit of information to be displayed in the UI, should have a corresponding field or property in the ViewModel (although they may be retrieved or derived from values in the Model) .. the ViewModel makes it easy to store the View State/Session State (such as the current selection of items in a UserList) which is not present the in the Model class behind.

Since you want to show the 'elapsed time since' value in the UI, your ViewModel should have a property caled ElapsedTimeSince. Your WPF View has a control which is data-bound to this property. Now as per your need, ensure you have a thread/timer event that re-evaluates the property value periodically using the current time and the Model's StartDate property. Your UI should reflect the updated value.

Gishu
I figured as much...
Michael Prewecki