private void PopulateGrids() { if (_monthView != null) { if (_dayTitleTemplate != null) { for (int i = 0; i < COLS; i++) { FrameworkElement titleCell = (FrameworkElement)this._dayTitleTemplate.LoadContent(); titleCell.SetValue(Grid.RowProperty, 0); titleCell.SetValue(Grid.ColumnProperty, i); this._monthView.Children.Add(titleCell); } } for (int i = 1; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { CalendarDayButton dayCell = new CalendarDayButton(); dayCell.Owner = this.Owner; dayCell.SetValue(Grid.RowProperty, i); dayCell.SetValue(Grid.ColumnProperty, j); dayCell.SetBinding(CalendarDayButton.StyleProperty, GetOwnerBinding("CalendarDayButtonStyle")); dayCell.AddHandler(CalendarDayButton.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Cell_MouseLeftButtonDown), true); dayCell.AddHandler(CalendarDayButton.MouseLeftButtonUpEvent, new MouseButtonEventHandler(Cell_MouseLeftButtonUp), true); dayCell.AddHandler(CalendarDayButton.MouseEnterEvent, new MouseEventHandler(Cell_MouseEnter), true); dayCell.AddHandler(CalendarDayButton.MouseLeaveEvent, new MouseEventHandler(Cell_MouseLeave), true); dayCell.Click += new RoutedEventHandler(Cell_Clicked); this._monthView.Children.Add(dayCell); } } } if (_yearView != null) { CalendarButton monthCell; int count = 0; for (int i = 0; i < YEAR_ROWS; i++) { for (int j = 0; j < YEAR_COLS; j++) { monthCell = new CalendarButton(); monthCell.Owner = this.Owner; monthCell.SetValue(Grid.RowProperty, i); monthCell.SetValue(Grid.ColumnProperty, j); monthCell.SetBinding(CalendarButton.StyleProperty, GetOwnerBinding("CalendarButtonStyle")); monthCell.AddHandler(CalendarButton.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Month_MouseLeftButtonDown), true); monthCell.AddHandler(CalendarButton.MouseLeftButtonUpEvent, new MouseButtonEventHandler(Month_MouseLeftButtonUp), true); monthCell.AddHandler(CalendarButton.MouseEnterEvent, new MouseEventHandler(Month_MouseEnter), true); monthCell.Click += new RoutedEventHandler(Month_Clicked); this._yearView.Children.Add(monthCell); count++; } } } }
private void AddMonthModeHighlight() { var owner = this.Owner; if (owner == null) { return; } if (owner.HoverStart.HasValue && owner.HoverEnd.HasValue) { DateTime hStart = owner.HoverEnd.Value; DateTime hEnd = owner.HoverEnd.Value; int daysToHighlight = DateTimeHelper.CompareDays(owner.HoverEnd.Value, owner.HoverStart.Value); if (daysToHighlight < 0) { hEnd = this.Owner.HoverStart.Value; } else { hStart = this.Owner.HoverStart.Value; } int count = ROWS * COLS; for (int childIndex = COLS; childIndex < count; childIndex++) { CalendarDayButton childButton = _monthView.Children[childIndex] as CalendarDayButton; if (childButton.DataContext is DateTime) { DateTime date = (DateTime)childButton.DataContext; childButton.SetValue( CalendarDayButton.IsHighlightedPropertyKey, (daysToHighlight != 0) && DateTimeHelper.InRange(date, hStart, hEnd)); } else { childButton.SetValue(CalendarDayButton.IsHighlightedPropertyKey, false); } } } else { int count = ROWS * COLS; for (int childIndex = COLS; childIndex < count; childIndex++) { CalendarDayButton childButton = _monthView.Children[childIndex] as CalendarDayButton; childButton.SetValue(CalendarDayButton.IsHighlightedPropertyKey, false); } } }
private void SetMonthModeDayButtonState(CalendarDayButton childButton, DateTime?dateToAdd) { if (this.Owner != null) { if (dateToAdd.HasValue) { childButton.Visibility = Visibility.Visible; // If the day is outside the DisplayDateStart/End boundary, do not show it if (DateTimeHelper.CompareDays(dateToAdd.Value, this.Owner.DisplayDateStartInternal) < 0 || DateTimeHelper.CompareDays(dateToAdd.Value, this.Owner.DisplayDateEndInternal) > 0) { childButton.IsEnabled = false; childButton.Visibility = Visibility.Hidden; } else { childButton.IsEnabled = true; // SET IF THE DAY IS SELECTABLE OR NOT childButton.SetValue( CalendarDayButton.IsBlackedOutPropertyKey, this.Owner.BlackoutDates.Contains(dateToAdd.Value)); // SET IF THE DAY IS ACTIVE OR NOT: set if the day is a trailing day or not childButton.SetValue( CalendarDayButton.IsInactivePropertyKey, DateTimeHelper.CompareYearMonth(dateToAdd.Value, this.Owner.DisplayDateInternal) != 0); // SET IF THE DAY IS TODAY OR NOT if (DateTimeHelper.CompareDays(dateToAdd.Value, DateTime.Today) == 0) { childButton.SetValue(CalendarDayButton.IsTodayPropertyKey, true); // Calendar.IsTodayHighlighted affects the final visual state for Today buttons // but childButton property change callbacks are no called in response to // Calendar.IsTodayHighlighted changing so we must explicitly update the visual state childButton.ChangeVisualState(true); } else { childButton.SetValue(CalendarDayButton.IsTodayPropertyKey, false); } // SET IF THE DAY IS SELECTED OR NOT // Since we should be comparing the Date values not DateTime values, we can't use this.Owner.SelectedDates.Contains(dateToAdd) directly bool isSelected = false; foreach (DateTime item in this.Owner.SelectedDates) { isSelected |= (DateTimeHelper.CompareDays(dateToAdd.Value, item) == 0); } childButton.SetValue(CalendarDayButton.IsSelectedPropertyKey, isSelected); } } else { childButton.Visibility = Visibility.Hidden; childButton.IsEnabled = false; childButton.SetValue(CalendarDayButton.IsBlackedOutPropertyKey, false); childButton.SetValue(CalendarDayButton.IsInactivePropertyKey, true); childButton.SetValue(CalendarDayButton.IsTodayPropertyKey, false); childButton.SetValue(CalendarDayButton.IsSelectedPropertyKey, false); } } }
private void PopulateGrids() { if (_monthView != null) { if (_dayTitleTemplate != null) { for (int i = 0; i < COLS; i++) { FrameworkElement titleCell = (FrameworkElement)this._dayTitleTemplate.LoadContent(); titleCell.SetValue(Grid.RowProperty, 0); titleCell.SetValue(Grid.ColumnProperty, i); this._monthView.Children.Add(titleCell); } } for (int i = 1; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { CalendarDayButton dayCell = new CalendarDayButton(); dayCell.Owner = this.Owner; dayCell.SetValue(Grid.RowProperty, i); dayCell.SetValue(Grid.ColumnProperty, j); dayCell.SetBinding(CalendarDayButton.StyleProperty, GetOwnerBinding("CalendarDayButtonStyle")); dayCell.AddHandler(CalendarDayButton.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Cell_MouseLeftButtonDown), true); dayCell.AddHandler(CalendarDayButton.MouseLeftButtonUpEvent, new MouseButtonEventHandler(Cell_MouseLeftButtonUp), true); dayCell.AddHandler(CalendarDayButton.MouseEnterEvent, new MouseEventHandler(Cell_MouseEnter), true); dayCell.Click += new RoutedEventHandler(Cell_Clicked); dayCell.AddHandler(PreviewKeyDownEvent, new RoutedEventHandler(CellOrMonth_PreviewKeyDown), true); this._monthView.Children.Add(dayCell); } } } if (_yearView != null) { CalendarButton monthCell; int count = 0; for (int i = 0; i < YEAR_ROWS; i++) { for (int j = 0; j < YEAR_COLS; j++) { monthCell = new CalendarButton(); monthCell.Owner = this.Owner; monthCell.SetValue(Grid.RowProperty, i); monthCell.SetValue(Grid.ColumnProperty, j); monthCell.SetBinding(CalendarButton.StyleProperty, GetOwnerBinding("CalendarButtonStyle")); monthCell.AddHandler(CalendarButton.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Month_MouseLeftButtonDown), true); monthCell.AddHandler(CalendarButton.MouseLeftButtonUpEvent, new MouseButtonEventHandler(Month_MouseLeftButtonUp), true); monthCell.AddHandler(CalendarButton.MouseEnterEvent, new MouseEventHandler(Month_MouseEnter), true); monthCell.AddHandler(UIElement.PreviewKeyDownEvent, new RoutedEventHandler(CellOrMonth_PreviewKeyDown), true); monthCell.Click += new RoutedEventHandler(Month_Clicked); this._yearView.Children.Add(monthCell); count++; } } } }
private void SetMonthModeDayButtonState(CalendarDayButton childButton, DateTime? dateToAdd) { if (this.Owner != null) { if (dateToAdd.HasValue) { childButton.Visibility = Visibility.Visible; // If the day is outside the DisplayDateStart/End boundary, do not show it if (DateTimeHelper.CompareDays(dateToAdd.Value, this.Owner.DisplayDateStartInternal) < 0 || DateTimeHelper.CompareDays(dateToAdd.Value, this.Owner.DisplayDateEndInternal) > 0) { childButton.IsEnabled = false; childButton.Visibility = Visibility.Hidden; } else { childButton.IsEnabled = true; // SET IF THE DAY IS SELECTABLE OR NOT childButton.SetValue( CalendarDayButton.IsBlackedOutPropertyKey, this.Owner.BlackoutDates.Contains(dateToAdd.Value)); // SET IF THE DAY IS ACTIVE OR NOT: set if the day is a trailing day or not childButton.SetValue( CalendarDayButton.IsInactivePropertyKey, DateTimeHelper.CompareYearMonth(dateToAdd.Value, this.Owner.DisplayDateInternal) != 0); // SET IF THE DAY IS TODAY OR NOT if (DateTimeHelper.CompareDays(dateToAdd.Value, DateTime.Today) == 0) { childButton.SetValue(CalendarDayButton.IsTodayPropertyKey, true); // Calendar.IsTodayHighlighted affects the final visual state for Today buttons // but childButton property change callbacks are no called in response to // Calendar.IsTodayHighlighted changing so we must explicitly update the visual state childButton.ChangeVisualState(true); } else { childButton.SetValue(CalendarDayButton.IsTodayPropertyKey, false); } // SET IF THE DAY IS SELECTED OR NOT // Since we should be comparing the Date values not DateTime values, we can't use this.Owner.SelectedDates.Contains(dateToAdd) directly bool isSelected = false; foreach (DateTime item in this.Owner.SelectedDates) { isSelected |= (DateTimeHelper.CompareDays(dateToAdd.Value, item) == 0); } childButton.SetValue(CalendarDayButton.IsSelectedPropertyKey, isSelected); } } else { childButton.Visibility = Visibility.Hidden; childButton.IsEnabled = false; childButton.SetValue(CalendarDayButton.IsBlackedOutPropertyKey, false); childButton.SetValue(CalendarDayButton.IsInactivePropertyKey, true); childButton.SetValue(CalendarDayButton.IsTodayPropertyKey, false); childButton.SetValue(CalendarDayButton.IsSelectedPropertyKey, false); } } }