title: UWP CalendarView update on command
body: <p>I am building a Windows Phone 10 application using Universal Windows Platform. In my app I have a standard CalendarView that I would like to show density colors on for dates that have events. The idea is to load the calendar as soon as the page is loaded, make an API request, and upon successful data retrieval have the CalendarView refresh it's UI so that the <code>CalendarViewDayItemChanging</code> event is called. From there I can set my density colors for the cells that have events.</p>
  
  <p>I have pretty much everything working correctly except for one part. When the calendar first loads I set it's min/max date ranges to the current month so that we only see one month at a time. This causes the calendar's UI to refresh as expected. However, after my API request completes if I try to set the min/max date ranges again, to the same dates, then the calendar does not refresh it's UI. Due to this I have no way to force the CalendarView to refresh it's UI. </p>
  
  <p>I've tried calling UpdateLayout, I've tried to reset the min/max date ranges, and I've tried to bind the calendar's DataContext to an ObservableCollection in my code behind that is updated when my data updates. None of this works and I do not see any method to just update the UI.</p>
  
  <p>I'm pretty new to UWP so am unsure of what I am doing wrong. I know the concept of Data Binding is a big part of UWP, but I am unsure how I would bind my data to this CalendarView so that it refreshes when my data is refreshed. Any suggestions?</p>
  
  <p>Below is a quick excerpt of my code as it stands now.</p>
  
  <p>XAML</p>
  
  <pre><code>&lt;CalendarView 
      Name="Calendar"
      NumberOfWeeksInView="6"
      CalendarViewDayItemChanging="CalendarView_DayItemChanging"
      DataContext="{Binding CalendarDates}"&gt;
  &lt;/CalendarView&gt;
  </code></pre>
  
  <p>Code-behind</p>
  
  <pre><code>namespace Pages
  {
      public sealed partial class CalendarPage : BasePage
      {
          #region Private Variables
  
          private CalendarPageModel PageModel = new CalendarPageModel();
          private ObservableCollection&lt;DateTime&gt; CalendarDates;
  
          #endregion
  
          #region Constructor
  
          public CalendarPage()
          {
              this.InitializeComponent();
              CalendarDates = new ObservableCollection&lt;DateTime&gt;();
          }
  
          #endregion
  
          #region Events
  
          private void Page_Loaded(object sender, RoutedEventArgs args)
          {
              SetCalendarDateRange(); //NOTE: This is done here so that my UI consistantly shows the correct dates on the screen
              LoadData();
          }
  
          private void CalendarView_DayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
          {
              if (!PageModel.DateHasEvent(args.Item.Date))
              {
                  args.Item.SetDensityColors(null);
              }
              else
              {
                  List&lt;Color&gt; colors = new List&lt;Color&gt;();
                  Color? color = Application.Current.Resources["CalendarHasEventDensityColor"] as Color?;
                  if (color != null)
                  {
                      colors.Add((Color)color);
                  }
  
                  args.Item.SetDensityColors(colors);
              }
          }
  
          #endregion
  
          #region Data
  
          private void SetCalendarDateRange()
          {
              Calendar.MinDate = PageModel.StartDate;
              Calendar.MaxDate = PageModel.EndDate;
          }
  
          private async void LoadData()
          {
              // get data
              await PageModel.RefreshData(PageModel.StartDate, PageModel.EndDate);
  
              // force calendar to update
              //NOTE: This only works if the date range is actually different than what it's currently set to
              SetCalendarDateRange();
  
              //NOTE: I have tried to just manually add a date to my observable collection to see if it'll kick off the calendar refresh, but it doesn't
              CalendarDates.add(DateTime.Now);
          }
  
          #endregion
      }
  }
  </code></pre>