title: WPF User control: can I bind properties from an internal and an external datacontext?
body: <p>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</p>
  
  <pre><code>public class CalendarDay
  {
      public int DayNumber {get; set;}
      public List&lt;DayItem&gt; Items {get; set;}
  }
  </code></pre>
  
  <p>where a <code>DayItem</code> could represent something like an appointment or a todo.</p>
  
  <p>I'm implementing this as a user control. The XAML for this calendar is a <code>ControlTemplate</code> that contains a <code>UniformGrid</code> of 1x7 for the day names (data bound to a collection 7 strings) and a <code>UniformGrid</code> of 6x7 for the days (data bound to a collection of <code>CalendarDay</code>).</p>
  
  <p>A view (user control) that <em>contains</em> this calendar conceptually looks like this:</p>
  
  <pre><code>&lt;UserControl name="myView" ... xmlns:cal="clr-namespace:the calendar namespace"&gt;
  
      &lt;Grid&gt;
          &lt;cal:Calendar Days="{Binding DaysWithItems}" CurrentMonth="{Binding DisplayMonth}" /&gt;
      &lt;/Grid&gt;
  
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>As I'm applying MVVM, <code>myView</code> will have a DataContext that is set to some view model class that has a property <code>DaysWithItems</code> (a list of <code>CalenderDay</code> instances) and a property <code>DisplayMonth</code>.</p>
  
  <p>Ideally, the consumer of this calendar control should only have to provide the two inputs as mentioned. Moreover, <code>DaysWithItems</code> should, from <code>myView</code>'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 <code>myView</code>'s view model.</p>
  
  <p>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.</p>
  
  <p>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 <code>CalendarDay</code>, the datacontext should be the <code>Calendar</code> class itself (because of the responsibilities I explained earlier).
  On the other hand, in <code>myView</code>, I'm binding the calendar to <code>myView</code>'s <code>DaysWithItems</code> (the (logical) collection that contains 28..31 elements), so there the calendar's datacontext should be <code>myView</code>'s view model.</p>
  
  <p>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)?</p>