I am trying to build my own calendar control in WPF/XAML, both as an exercise and to be used in a hobby project. This calender will be a grid where each cell obviously represents a day in the chosen month. Each cell should be able to display a list of items. The inputs for the calendar should be an identification of a month and a list of days. Those "days" will be of a custom type, something like
public class CalendarDay
{
public int DayNumber {get; set;}
public List<DayItem> Items {get; set;}
}
where a DayItem
could represent something like an appointment or a todo.
I'm implementing this as a user control. The XAML for this calendar is a ControlTemplate
that contains a UniformGrid
of 1x7 for the day names (data bound to a collection 7 strings) and a UniformGrid
of 6x7 for the days (data bound to a collection of CalendarDay
).
A view (user control) that contains this calendar conceptually looks like this:
<UserControl name="myView" ... xmlns:cal="clr-namespace:the calendar namespace">
<Grid>
<cal:Calendar Days="{Binding DaysWithItems}" CurrentMonth="{Binding DisplayMonth}" />
</Grid>
</UserControl>
As I'm applying MVVM, myView
will have a DataContext that is set to some view model class that has a property DaysWithItems
(a list of CalenderDay
instances) and a property DisplayMonth
.
Ideally, the consumer of this calendar control should only have to provide the two inputs as mentioned. Moreover, DaysWithItems
should, from myView
's point of view (pun is coincidental), be a list of 28, 29, 30 or 31 elements, depending on the month. This means that the list should somehow be padded to 42 items. I think this should be the responsibility of the calendar control, not myView
's view model.
Note that I'm also not provding the day names. This too should be the responsibility of the calendar control. This shouldn't be provided explicitly.
Here's my problem. If, in the calendar's control template, I want to bind to the string collection for the day names and the 42 element collection of CalendarDay
, the datacontext should be the Calendar
class itself (because of the responsibilities I explained earlier).
On the other hand, in myView
, I'm binding the calendar to myView
's DaysWithItems
(the (logical) collection that contains 28..31 elements), so there the calendar's datacontext should be myView
's view model.
Can I use some sort of internal datacontext (= "internal" to the control template) and also some sort of external datacontext (= a datacontext as provided by the consumer of the calendar control)?