private void BuildItems() { if (_itemsGrid != null) { AddDefaultItems(); var startOfMonth = new DateTime(_year, _month, 1); DayOfWeek dayOfWeek = startOfMonth.DayOfWeek; var daysInMonth = (int)Math.Floor(startOfMonth.AddMonths(1).Subtract(startOfMonth).TotalDays); var addedDays = 0; int lastWeekNumber = 0; for (int rowCount = 1; rowCount <= RowCount; rowCount++) { for (var columnCount = 1; columnCount < ColumnCount; columnCount++) { int rowCounter = rowCount; int columnCounter = columnCount; var item = (CalendarItem)(from oneChild in _itemsGrid.Children where oneChild is CalendarItem && ((CalendarItem)oneChild).Tag.ToString() == string.Concat(rowCounter.ToString(), ":", columnCounter.ToString()) select oneChild).FirstOrDefault(); if (item != null) { if (rowCount == 1 && columnCount < (int) dayOfWeek + 1) { item.Visibility = Visibility.Collapsed; } else if (addedDays < daysInMonth) { item.Visibility = Visibility.Visible; } else { item.Visibility = Visibility.Collapsed; } int count = rowCount; var weekItem = (CalendarWeekItem) (from oneChild in _itemsGrid.Children where oneChild is CalendarWeekItem && ((CalendarWeekItem) oneChild).Tag.ToString() == string.Concat(count.ToString(), ":0") select oneChild).FirstOrDefault(); if (item.Visibility == Visibility.Visible) { item.ItemDate = startOfMonth.AddDays(addedDays); if (SelectedDate == DateTime.MinValue && item.ItemDate == DateTime.Today) { SelectedDate = item.ItemDate; if (ShowSelectedDate) item.IsSelected = true; _lastItem = item; } else { if (item.ItemDate == SelectedDate) { if (ShowSelectedDate) item.IsSelected = true; } else { item.IsSelected = false; } } addedDays += 1; item.DayNumber = addedDays; item.DataContext = FindItem(item.ItemDate); if (WeekNumberDisplay != WeekNumberDisplayOption.None) { int weekNumber; if (WeekNumberDisplay == WeekNumberDisplayOption.WeekOfYear) { var systemCalendar = CultureInfo.CurrentCulture.Calendar; weekNumber = systemCalendar.GetWeekOfYear( item.ItemDate, CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule, CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek); } else { weekNumber = rowCount; } weekItem.WeekNumber = weekNumber; lastWeekNumber = weekNumber; weekItem.Visibility = Visibility.Visible; } } else { if (WeekNumberDisplay != WeekNumberDisplayOption.None && weekItem.WeekNumber != lastWeekNumber) { weekItem.Visibility = Visibility.Collapsed; } } } } } } }
private void AddDefaultItems() { if (!_addedItems && _itemsGrid != null) { for (int rowCount = 1; rowCount <= RowCount; rowCount++) { for (int columnCount = 1; columnCount < ColumnCount; columnCount++) { var item = new CalendarItem(this); item.SetValue(Grid.RowProperty, rowCount); item.SetValue(Grid.ColumnProperty, columnCount); item.Visibility = Visibility.Collapsed; item.Tag = string.Concat(rowCount.ToString(), ":", columnCount.ToString()); item.Tapped += ItemTapped; if (CalendarItemStyle != null) { item.Style = CalendarItemStyle; } _itemsGrid.Children.Add(item); } if (WeekNumberDisplay != WeekNumberDisplayOption.None) { const int columnCount = 0; var item = new CalendarWeekItem(); item.SetValue(Grid.RowProperty, rowCount); item.SetValue(Grid.ColumnProperty, columnCount); item.Visibility = Visibility.Collapsed; item.Tag = string.Concat(rowCount.ToString(), ":", columnCount.ToString()); if (CalendarWeekItemStyle != null) { item.Style = CalendarWeekItemStyle; } _itemsGrid.Children.Add(item); } } _addedItems = true; } }
//private void IncrementYear() //{ // if (_year != 2499) // { // _year += 1; // SetYearMonthLabel(); // } //} //private void DecrementYear() //{ // if (_year != 1753) // { // _year -= 1; // SetYearMonthLabel(); // } //} private void ProcessSelectedItem(object sender) { if (_lastItem != null) { _lastItem.IsSelected = false; } _lastItem = (sender as CalendarItem); if (_lastItem != null) { if (ShowSelectedDate) _lastItem.IsSelected = true; SelectedDate = _lastItem.ItemDate; } }