//rebinds to the dates void ReBindListOfDays() { if (datesList != null) { //Please note that the DateHelper.GetDaysOfMonth gets the days from a cache so it will not have a performance hit to call it everytime int numberOfDaysFromPreviousMonth = (int)DateHelper.GetDayOfWeek(currentlyViewedYear, currentlyViewedMonth, 1); DayCell[] newDaysTemp = dateHelper.GetDaysOfMonth(currentlyViewedMonth, currentlyViewedYear, MinDate, MaxDate); //get the last day of the month and determine the number of days to show from next month int numberOfDaysFromNextMonth = 6 - (int)DateHelper.GetDayOfWeek(currentlyViewedYear, currentlyViewedMonth, newDaysTemp[newDaysTemp.Length - 1].DayNumber); DayCell[] newDays = new DayCell[newDaysTemp.Length + numberOfDaysFromNextMonth]; int monthToGetNext; int yearTogetNext; //get the next month DateHelper.MoveMonthForward(currentlyViewedMonth, currentlyViewedYear, out monthToGetNext, out yearTogetNext); //get the data for next month DayCell[] nextDays = dateHelper.GetDaysOfMonth(monthToGetNext, yearTogetNext, MinDate, MaxDate); //get the next month newDaysTemp.CopyTo(newDays, 0); //copy the new days array Array.Copy(nextDays, 0, newDays, newDaysTemp.Length, newDays.Length - newDaysTemp.Length); DayCell[] listOfDays = new DayCell[numberOfDaysFromPreviousMonth + newDays.Length]; int monthToGetPrevious; int yearTogetPrevious; //move one month back DateHelper.MoveMonthBack(currentlyViewedMonth, currentlyViewedYear, out monthToGetPrevious, out yearTogetPrevious); DayCell[] oldDays = dateHelper.GetDaysOfMonth(monthToGetPrevious, yearTogetPrevious, MinDate, MaxDate);//get the previous month Array.Copy(oldDays, oldDays.Length - numberOfDaysFromPreviousMonth, listOfDays, 0, numberOfDaysFromPreviousMonth); Array.Copy(newDays, 0, listOfDays, numberOfDaysFromPreviousMonth, newDays.Length); //set the item source to the days to show datesList.ItemsSource = listOfDays; } }
/// <summary> /// Generates an array of days in a month /// </summary> /// <param name="month">The month to get the days for </param> /// <param name="year">The year to get the days for </param> /// <param name="maxDate">The max date to generate enabled cells</param> /// <param name="minDate">The min date to generate enabled cells</param> /// <returns>Returns an int array full of days in a month</returns> public DayCell[] GetDaysOfMonth(int month, int year, DateTime minDate, DateTime maxDate) { KeyValuePair<int, int> key = new KeyValuePair<int, int>(month, year); int daysCount = DateTime.DaysInMonth(year, month); DayCell[] days = null; if (daysArrays.ContainsKey(key)) { days = daysArrays[key]; foreach (DayCell item in days) item.IsEnabled = IsDateWithinRange(minDate, maxDate, item); } else { days = new DayCell[daysCount]; for (int i = 0; i < days.Length; i++) { DayCell item = new DayCell(i + 1, month, year); item.IsEnabled = IsDateWithinRange(minDate, maxDate, item); days[i] = item; } daysArrays[key] = days;//cache the array } return days; }
//rebinds to the dates void ReBindListOfDays() { if (datesList != null) { //Please note that the DateHelper.GetDaysOfMonth gets the days from a cache so it will not have a performance hit to call it everytime int numberOfDaysFromPreviousMonth = (int)DateHelper.GetDayOfWeek(currentlyViewedYear, currentlyViewedMonth, 1); DayCell[] newDaysTemp = dateHelper.GetDaysOfMonth(currentlyViewedMonth, currentlyViewedYear, MinDate, MaxDate); //get the last day of the month and determine the number of days to show from next month int numberOfDaysFromNextMonth = 6 - (int)DateHelper.GetDayOfWeek(currentlyViewedYear, currentlyViewedMonth, newDaysTemp[newDaysTemp.Length - 1].DayNumber); DayCell[] newDays = new DayCell[newDaysTemp.Length + numberOfDaysFromNextMonth]; int monthToGetNext; int yearTogetNext; //get the next month DateHelper.MoveMonthForward(currentlyViewedMonth, currentlyViewedYear, out monthToGetNext, out yearTogetNext); //get the data for next month DayCell[] nextDays = dateHelper.GetDaysOfMonth(monthToGetNext, yearTogetNext, MinDate, MaxDate);//get the next month newDaysTemp.CopyTo(newDays, 0);//copy the new days array Array.Copy(nextDays, 0, newDays, newDaysTemp.Length, newDays.Length - newDaysTemp.Length); DayCell[] listOfDays = new DayCell[numberOfDaysFromPreviousMonth + newDays.Length]; int monthToGetPrevious; int yearTogetPrevious; //move one month back DateHelper.MoveMonthBack(currentlyViewedMonth, currentlyViewedYear, out monthToGetPrevious, out yearTogetPrevious); DayCell[] oldDays = dateHelper.GetDaysOfMonth(monthToGetPrevious, yearTogetPrevious, MinDate, MaxDate);//get the previous month Array.Copy(oldDays, oldDays.Length - numberOfDaysFromPreviousMonth, listOfDays, 0, numberOfDaysFromPreviousMonth); Array.Copy(newDays, 0, listOfDays, numberOfDaysFromPreviousMonth, newDays.Length); //set the item source to the days to show datesList.ItemsSource = listOfDays; foreach (var day in listOfDays) { if (day.YearNumber == CurrentlySelectedDate.Year && day.MonthNumber == CurrentlySelectedDate.Month && day.DayNumber == CurrentlySelectedDate.Day) { datesList.SelectedItem = day; } } } }
DateTime GetDateFromCell(DayCell cell) { return new DateTime(cell.YearNumber, cell.MonthNumber, cell.DayNumber); }
DateTime GetDateFromCell(DayCell cell) { return(new DateTime(cell.YearNumber, cell.MonthNumber, cell.DayNumber)); }
/// <summary> /// Checks if the specified date is greater than /// </summary> /// <param name="minDate">The min date</param> /// <param name="maxDate">The max date</param> /// <param name="cell">The cell to check</param> /// <returns>Returns true if the cell is greater</returns> public static bool IsDateWithinRange(DateTime minDate, DateTime maxDate, DayCell cell) { long ticks = new DateTime(cell.YearNumber, cell.MonthNumber, cell.DayNumber).Ticks; return ticks >= minDate.Ticks && ticks <= maxDate.Ticks; }