private void PopulateGrids() { if (_monthGrid != null) { for (int i = 0; i < COLS; i++) { if (_dayTitleTemplate != null) { FrameworkElement cell = (FrameworkElement)this._dayTitleTemplate.LoadContent(); cell.SetValue(Grid.RowProperty, 0); cell.SetValue(Grid.ColumnProperty, i); this._monthGrid.Children.Add(cell); } } for (int i = 1; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { DayButton cell = new DayButton(); cell.SetValue(Grid.RowProperty, i); cell.SetValue(Grid.ColumnProperty, j); ((DayButton)cell).Click += new RoutedEventHandler(Cell_Click); if (_owner.DayStyle != null) { cell.Style = _owner.DayStyle; } this._monthGrid.Children.Add(cell); } } } if (_yearViewGrid != null) { MonthButton month; int count = 0; for (int i = 0; i < YEAR_ROWS; i++) { for (int j = 0; j < YEAR_COLS; j++) { month = new MonthButton(); month.SetValue(Grid.RowProperty, i); month.SetValue(Grid.ColumnProperty, j); month.MouseLeftButtonUp += new MouseButtonEventHandler(Month_MouseLeftButtonUp); if (_owner.MonthStyle != null) { month.Style = _owner.MonthStyle; } this._yearViewGrid.Children.Add(month); count++; } } } }
private void Cell_Click(object sender, RoutedEventArgs e) { _owner.Focus(); DayButton b = sender as DayButton; if (b != null && b.IsEnabled) { DateTime selectedDate = (DateTime)b.DataContext; _owner.OnDayClick(selectedDate); } }
public void UpdateSelectedDate(DateTime?addedDate, DateTime?removedDate) { int count = 1; if (addedDate != removedDate && _monthGrid != null) { foreach (object child in _monthGrid.Children) { if (count > COLS) { DayButton mybutton = child as DayButton; Debug.Assert(mybutton != null); DateTime current = (DateTime)mybutton.DataContext; if (current != null) { if (addedDate != null) { if (DateTime.Compare(current, (DateTime)addedDate) == 0) { if (_currentButton != null) { _currentButton.IsCurrent = false; _currentButton.IsTabStop = false; } mybutton.IsSelected = true; mybutton.IsTabStop = true; mybutton.IsCurrent = true; _currentButton = mybutton; } } if (removedDate != null) { if (DateTime.Compare(current, (DateTime)removedDate) == 0) { mybutton.IsSelected = false; mybutton.IsTabStop = false; mybutton.IsCurrent = false; } } } } count++; } } }
public void UpdateMonthMode() { Debug.Assert(this._owner.DisplayDate != null); _currentMonth = (DateTime)this._owner.DisplayDate; _firstDayOfWeek = this._owner.FirstDayOfWeek; if (_currentMonth != null) { int year = _calendar.GetYear(_currentMonth); int month = _calendar.GetMonth(_currentMonth); DateTime firstDayOfMonth = new DateTime(year, month, 1); if (this._headerButton != null) { this._headerButton.Content = string.Format(CultureInfo.CurrentCulture, Resource.Calendar_MonthViewHeaderText, CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[month - 1], year.ToString(CultureInfo.CurrentCulture)); this._headerButton.IsEnabled = true; } int lastMonthToDisplay = PreviousMonthDays(firstDayOfMonth); DateTime dateToAdd = _calendar.AddDays(firstDayOfMonth, -lastMonthToDisplay); DateTime firstDayOfNextMonth = _calendar.AddMonths(firstDayOfMonth, 1); // check to see if Prev/Next Buttons will be displayed if (_previousButton != null) { if (DateTime.Compare(_owner.DisplayDateStart.GetValueOrDefault(DateTime.MinValue), firstDayOfMonth) > -1) { _previousButton.IsEnabled = false; } else { _previousButton.IsEnabled = true; } } if (_nextButton != null) { if (DateTime.Compare(_owner.DisplayDateEnd.GetValueOrDefault(DateTime.MaxValue), firstDayOfNextMonth) < 0) { _nextButton.IsEnabled = false; } else { _nextButton.IsEnabled = true; } } int count = 0; if (_monthGrid != null) { Debug.Assert(_monthGrid.Children.Count == COLS * ROWS); //Set the day titles foreach (object child in _monthGrid.Children) { if (count < (COLS)) { //this assumes that the day titles are always text blocks TextBlock daytitle = child as TextBlock; Debug.Assert(daytitle != null); daytitle.Text = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedDayNames[(count + (int)_firstDayOfWeek) % NUMBER_OF_DAYS_IN_WEEK]; } else { DayButton mybutton = child as DayButton; Debug.Assert(mybutton != null); SetButtonState(mybutton, dateToAdd); mybutton.IsTabStop = false; if (_currentButton == null) { if (_owner.SelectedDate == null && mybutton.IsToday) { mybutton.IsTabStop = true; _currentButton = mybutton; mybutton.IsCurrent = true; } else { if (mybutton.IsSelected) { mybutton.IsTabStop = true; _currentButton = mybutton; mybutton.IsCurrent = true; } } } mybutton.Content = String.Format(CultureInfo.CurrentCulture, "{0,2}", dateToAdd.Day); mybutton.DataContext = dateToAdd; dateToAdd = _calendar.AddDays(dateToAdd, 1); } count++; } } } }
private void SetButtonState(DayButton mybutton, DateTime dateToAdd) { mybutton.Opacity = 1; if (DateTime.Compare(dateToAdd, _owner.DisplayDateStart.GetValueOrDefault(DateTime.MinValue)) < 0 || DateTime.Compare(dateToAdd, _owner.DisplayDateEnd.GetValueOrDefault(DateTime.MaxValue)) > 0) { mybutton.IsEnabled = false; mybutton.Opacity = 0; } else { //SET IF THE DAY IS SELECTABLE OR NOT if (_owner.SelectableDateEnd != null || _owner.SelectableDateStart != null) { if (DateTime.Compare(dateToAdd, _owner.SelectableDateEnd.GetValueOrDefault(DateTime.MaxValue)) < 1 && DateTime.Compare(dateToAdd, _owner.SelectableDateStart.GetValueOrDefault(DateTime.MinValue)) > -1) { mybutton.IsDisabled = false; mybutton.IsEnabled = true; } else { mybutton.IsDisabled = true; mybutton.IsEnabled = false; } } else { if (_owner.AreDatesInPastSelectable) { mybutton.IsDisabled = false; mybutton.IsEnabled = true; } else { if (DateTime.Compare(dateToAdd, DateTime.Today) > -1) { mybutton.IsDisabled = false; mybutton.IsEnabled = true; } else { mybutton.IsDisabled = true; mybutton.IsEnabled = false; } } } //SET IF THE DAY IS INACTIVE OR NOT if (CompareYearMonth(dateToAdd, (DateTime)_owner.DisplayDate) != 0) { mybutton.IsInactive = true; } else { mybutton.IsInactive = false; } //SET IF THE DAY IS TODAY OR NOT if (_owner.IsTodayHighlighted && dateToAdd == DateTime.Today) { mybutton.IsToday = true; } else { mybutton.IsToday = false; } //SET IF THE DAY IS SELECTED OR NOT if (DateTime.Compare(dateToAdd, _owner.SelectedDate.GetValueOrDefault(DateTime.MinValue)) == 0) { mybutton.IsSelected = true; mybutton.IsTabStop = true; mybutton.IsCurrent = true; _currentButton = mybutton; } else { mybutton.IsSelected = false; mybutton.IsTabStop = false; mybutton.IsCurrent = false; } } }
private static void EnsureDayStyle(DayButton day, Style oldCalendarDayStyle, Style newCalendarDayStyle) { Debug.Assert(day != null); if (newCalendarDayStyle != null) { // if (day != null && (day.Style == null || day.Style == oldCalendarDayStyle)) { day.Style = newCalendarDayStyle; } } }
public void UpdateMonthMode() { Debug.Assert(this._owner.DisplayDate != null); _currentMonth = (DateTime)this._owner.DisplayDate; _firstDayOfWeek = this._owner.FirstDayOfWeek; if ( _currentMonth != null) { int year = _calendar.GetYear(_currentMonth); int month = _calendar.GetMonth(_currentMonth); DateTime firstDayOfMonth = new DateTime(year, month, 1); if (this._headerButton != null) { this._headerButton.Content = string.Format(CultureInfo.CurrentCulture, Resource.Calendar_MonthViewHeaderText, CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[month - 1], year.ToString(CultureInfo.CurrentCulture)); this._headerButton.IsEnabled = true; } int lastMonthToDisplay = PreviousMonthDays(firstDayOfMonth); DateTime dateToAdd = _calendar.AddDays(firstDayOfMonth, -lastMonthToDisplay); DateTime firstDayOfNextMonth = _calendar.AddMonths(firstDayOfMonth, 1); // check to see if Prev/Next Buttons will be displayed if (_previousButton != null) { if (DateTime.Compare(_owner.DisplayDateStart.GetValueOrDefault(DateTime.MinValue), firstDayOfMonth) > -1) { _previousButton.IsEnabled = false; } else { _previousButton.IsEnabled = true; } } if (_nextButton != null) { if (DateTime.Compare(_owner.DisplayDateEnd.GetValueOrDefault(DateTime.MaxValue), firstDayOfNextMonth) < 0) { _nextButton.IsEnabled = false; } else { _nextButton.IsEnabled = true; } } int count = 0; if (_monthGrid != null) { Debug.Assert(_monthGrid.Children.Count == COLS * ROWS); //Set the day titles foreach (object child in _monthGrid.Children) { if (count < (COLS)) { //this assumes that the day titles are always text blocks TextBlock daytitle = child as TextBlock; Debug.Assert(daytitle != null); daytitle.Text = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedDayNames[(count + (int)_firstDayOfWeek) % NUMBER_OF_DAYS_IN_WEEK]; } else { DayButton mybutton = child as DayButton; Debug.Assert(mybutton != null); SetButtonState(mybutton, dateToAdd); mybutton.IsTabStop = false; if (_currentButton == null) { if (_owner.SelectedDate == null && mybutton.IsToday) { mybutton.IsTabStop = true; _currentButton = mybutton; mybutton.IsCurrent = true; } else { if (mybutton.IsSelected) { mybutton.IsTabStop = true; _currentButton = mybutton; mybutton.IsCurrent = true; } } } mybutton.Content = String.Format(CultureInfo.CurrentCulture, "{0,2}", dateToAdd.Day); mybutton.DataContext = dateToAdd; dateToAdd = _calendar.AddDays(dateToAdd, 1); } count++; } } } }
public void UpdateSelectedDate(DateTime? addedDate, DateTime? removedDate) { int count = 1; if (addedDate != removedDate && _monthGrid != null) { foreach (object child in _monthGrid.Children) { if (count > COLS) { DayButton mybutton = child as DayButton; Debug.Assert(mybutton != null); DateTime current = (DateTime)mybutton.DataContext; if (current != null) { if (addedDate != null) { if (DateTime.Compare(current, (DateTime)addedDate) == 0) { if (_currentButton != null) { _currentButton.IsCurrent = false; _currentButton.IsTabStop = false; } mybutton.IsSelected = true; mybutton.IsTabStop = true; mybutton.IsCurrent = true; _currentButton = mybutton; } } if (removedDate != null) { if (DateTime.Compare(current, (DateTime)removedDate) == 0) { mybutton.IsSelected = false; mybutton.IsTabStop = false; mybutton.IsCurrent = false; } } } } count++; } } }