Hierarchical ViewModels

janter

New member
Joined
Apr 25, 2019
Messages
4
Programming Experience
3-5
I've created a UserControl which is a Calendar. It's almost identical to the Outlook calendar.
Multiple calendars can be placed adjacent to each other to compare them, als shown in the image.

For viewing the calendar, the user can choose between a month view and a week view.
The month view displays all days of the current month in a uniform grid. Each cell in the grid contains the appointments for the day.
On the left side directly against the moth days grid, I display a strip with the week numbers. (Week numbers are not correct in the image, but doesn't matter)
1571942990505.png

(The header with day names is in Dutch language)

The week view displays the days of the current week in 7 columns, with the appointments in each column.
And on the left side a 'time strip' is placed, containing the time of the day in pieces of half hours.
1571943379968.png

I had a hard time designing the data structure. For example for the time strips.
In the month view I display week numbers, in the weeks view I display hours. First I wanted to use one general ViewModel for both views.
But for displaying the hours in the time strip of the weeks view, I needed a property TimeSlots on the ViewModel. While for the months view
I needed a WeekNumber property to display the week numbers.
So this is a specialization for each specific ViewModel, and when I would create one common general ViewModel for both view types, it would
become a property with an ugly, too general name, like TimeStripItems or something.
Also for the week view each item for the time strip is a TimeSpan and for the month view each item is just an integer for the week number.
In addition to that, I want to be able to display the items in the time strip of the week view in a dynamic resulotion. So in the future it must be possible
to adjust it to display not half hours, but whole hours, or even blocks of 15 minutes.
Then I made the decision to create two separate ViewModels. One for the week view, and one for the months view. Both contain the specific parts for their view.
Since the whole should represent one big Calendar UserControl, the data has to be mapped to the child UserControls (the week/month view).
But each view has its own DataContext (its own ViewModel), and so it can not inherit the DataContext of the big UserControl that contains them.

What are the best options to:
- Have a separation of logic (and specific properties) for the week and month view?
- Map the data like appointments to the child views?

The only solution I can think of, is to literally copy the calendar data to the child ViewModels (that are the ViewModels of the week/month view).
 
Back
Top Bottom