private void SetMonthModeDayButtonState(DayButton childButton, DateTime? dateToAdd) {
     if (Owner == null)
         return;
     if (!dateToAdd.HasValue) {
         childButton.Visibility = Visibility.Hidden;
         childButton.IsEnabled = false;
         childButton.SetValue(DayButton.IsBlackedOutPropertyKey, false);
         childButton.SetValue(DayButton.IsInactivePropertyKey, true);
         childButton.SetValue(DayButton.IsTodayPropertyKey, false);
         childButton.SetValue(DayButton.IsSelectedPropertyKey, false);
         return;
     }
     if (Engine.CompareDays(dateToAdd.Value, Owner.DisplayDateStartInternal) < 0
         || Engine.CompareDays(dateToAdd.Value, Owner.DisplayDateEndInternal) > 0) {
         childButton.IsEnabled = false;
         childButton.Visibility = Visibility.Hidden;
         return;
     }
     childButton.Visibility = Visibility.Visible;
     childButton.IsEnabled = true;
     childButton.SetValue(DayButton.IsBlackedOutPropertyKey, Owner.BlackoutDates.Contains(dateToAdd.Value));
     childButton.SetValue(DayButton.IsInactivePropertyKey, Engine.CompareYearMonth(dateToAdd.Value, Owner.DisplayDateInternal) != 0);
     childButton.SetValue(DayButton.IsTodayPropertyKey, Engine.CompareDays(dateToAdd.Value, DateTime.Today) == 0);
     childButton.NotifyNeedsVisualStateUpdate();
     var flag = false;
     // ReSharper disable LoopCanBeConvertedToQuery
     foreach (var dt2 in Owner.SelectedDates)
         // ReSharper restore LoopCanBeConvertedToQuery
         flag = flag | Engine.CompareDays(dateToAdd.Value, dt2) == 0;
     childButton.SetValue(DayButton.IsSelectedPropertyKey, flag);
 }
 private void PopulateMonthViewGrid() {
     if (_monthView == null)
         return;
     // todo short the loops and migrate to one loop
     for (var index = 0; index < 7; ++index) {
         var fe = _dayTitleTemplate != null ? (FrameworkElement)_dayTitleTemplate.LoadContent() : new ContentControl();
         fe.SetValue(Grid.RowProperty, 0);
         fe.SetValue(Grid.ColumnProperty, index);
         _monthView.Children.Add(fe);
     }
     for (var row = 1; row < 7; ++row) {
         for (var column = 0; column < 7; ++column) {
             var cdb = new DayButton { Owner = Owner };
             cdb.SetValue(Grid.RowProperty, row);
             cdb.SetValue(Grid.ColumnProperty, column);
             cdb.SetBinding(StyleProperty, GetOwnerBinding(Calendar.DayButtonStyleProperty));
             cdb.AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(Day_MouseLeftButtonDown), true);
             cdb.AddHandler(MouseLeftButtonUpEvent, new MouseButtonEventHandler(Day_MouseLeftButtonUp), true);
             cdb.AddHandler(MouseEnterEvent, new MouseEventHandler(Day_MouseEnter), true);
             cdb.Click += Day_Clicked;
             cdb.AddHandler(PreviewKeyDownEvent, new RoutedEventHandler(DayOrMonthOrYear_PreviewKeyDown), true);
             _monthView.Children.Add(cdb);
         }
     }
 }