private static object coerceDisplayDateStart(DependencyObject d, object o) { PersianCalendar pc = d as PersianCalendar; PersianDate value = (PersianDate)o; return(o); }
private void setYearMode() { this.monthUniformGrid.Visibility = this.decadeUniformGrid.Visibility = Visibility.Collapsed; this.yearUniformGrid.Visibility = Visibility.Visible; this.titleButton.Content = this.DisplayDate.Year.ToString(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { int month = j + i * 3 + 1; if (new PersianDate(DisplayDate.Year, month, PersianDate.DaysInMonth(DisplayDate.Year, month)) >= DisplayDateStart && new PersianDate(DisplayDate.Year, month, 1) <= DisplayDateEnd) { yearModeButtons[i, j].Content = ((PersianMonth)month).ToString(); yearModeButtons[i, j].IsEnabled = true; } else { yearModeButtons[i, j].Content = ""; yearModeButtons[i, j].IsEnabled = false; } } } }
private void setMonthMode() { this.decadeUniformGrid.Visibility = this.yearUniformGrid.Visibility = Visibility.Collapsed; this.monthUniformGrid.Visibility = Visibility.Visible; int year = DisplayDate.Year; int month = DisplayDate.Month; PersianDate firstDayInMonth = new PersianDate(year, month, 1); for (int i = 1; i <= 6; i++) { for (int j = 1; j <= 7; j++) { var button = monthModeButtons[i - 1, j - 1]; PersianDate date = new PersianDate(); bool dateInRange; try { //might throw OverflowException, which means that the date cannot be stored in PersianDate date = monthModeRowColumnToDate(i - 1, j - 1, firstDayInMonth); dateInRange = date >= DisplayDateStart && date <= DisplayDateEnd; } catch (OverflowException) { dateInRange = false; } if (dateInRange && date.Month == firstDayInMonth.Month) {//we're good! button.Foreground = Brushes.Black; button.Content = date.Day.ToString(); button.IsEnabled = true; button.Tag = date; } else if (dateInRange) {//belongs to the next, or the previous month button.Content = date.Day.ToString(); button.IsEnabled = true; button.Tag = date; button.Foreground = Brushes.LightGray; } else {//not in [DiplayDateStart, DiplayDateEnd] range button.Tag = null; button.Content = ""; button.IsEnabled = false; button.Background = Brushes.Transparent; } } } this.titleButton.Content = ((PersianMonth)month).ToString() + " " + year.ToString(); this.todayCheck(); this.selectedDateCheck(null); }
/// <param name="row">zero-based row number</param> /// <param name="column">zero-based column number</param> private static PersianDate monthModeRowColumnToDate(int row, int column, PersianDate displayDate) { int year = displayDate.Year; int month = displayDate.Month; PersianDate firstDay = new PersianDate(year, month, 1); int fstCol = 2 + (int)firstDay.PersianDayOfWeek; int fstRow = fstCol == 1 ? 2 : 1; int dayDifference = (row) * 7 + column + 1 - ((fstRow - 1) * 7 + fstCol); return(firstDay.AddDays(dayDifference)); }
private static object coerceDisplayDateEnd(DependencyObject d, object o) { PersianCalendar pc = d as PersianCalendar; PersianDate value = (PersianDate)o; if (value < pc.DisplayDateStart) { return(pc.DisplayDateStart); } return(o); }
//property changed callbacks and value coercions private static object coerceDisplayDateEnd(DependencyObject d, object o) { var pdp = d as PersianDatePicker; PersianDate value = (PersianDate)o; if (value < pdp.DisplayDateStart) { return(pdp.DisplayDateStart); } return(o); }
private void validateText() { PersianDate date; if (PersianDate.TryParse(dateTextBox.Text, out date)) { this.SelectedDate = date; this.DisplayDate = date; } this.Text = this.SelectedDate.ToString(); }
/// <param name="row">zero-based row number</param> /// <param name="column">zero-based column number</param> private static void monthModeDateToRowColumn(PersianDate date, out int row, out int column) { int year = date.Year; int month = date.Month; PersianDate firstDay = new PersianDate(year, month, 1); int fstCol = 2 + (int)firstDay.PersianDayOfWeek; int fstRow = fstCol == 1 ? 2 : 1; row = (date.Day + fstCol - 2) / 7 + fstRow; column = (date.Day + fstCol - 1) % 7; column = column == 0 ? 7 : column; column--; row--; }
// Overrides the ConvertFrom method of TypeConverter. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { return(PersianDate.Parse(value as string)); } if (value is DateTime) { return(new PersianDate((DateTime)value)); } return(base.ConvertFrom(context, culture, value)); }
// Overrides the ConvertTo method of TypeConverter. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string)) { PersianDate pd = (PersianDate)value; return(value.ToString()); } if (destinationType == typeof(DateTime)) { PersianDate pd = (PersianDate)value; return(pd.ToDateTime()); } return(base.ConvertTo(context, culture, value, destinationType)); }
private static object coerceDateToBeInRange(DependencyObject d, object o) { PersianDatePicker pdp = d as PersianDatePicker; PersianDate value = (PersianDate)o; if (value < pdp.DisplayDateStart) { return(pdp.DisplayDateStart); } if (value > pdp.DisplayDateEnd) { return(pdp.DisplayDateEnd); } return(o); }
private void previousButton_Click(object sender, RoutedEventArgs e) { int m = this.DisplayDate.Month; int y = this.DisplayDate.Year; try { PersianDate newDisplayDate = DisplayDate; if (this.DisplayMode == CalendarMode.Month) { if (m == 1) { newDisplayDate = new PersianDate(y - 1, 12, PersianDate.DaysInMonth(y - 1, 12)); } else { newDisplayDate = new PersianDate(y, m - 1, PersianDate.DaysInMonth(y, m - 1)); } } else if (this.DisplayMode == CalendarMode.Year) { newDisplayDate = new PersianDate(y - 1, 12, PersianDate.DaysInMonth(y - 1, 12)); } else if (this.DisplayMode == CalendarMode.Decade) { newDisplayDate = new PersianDate(y - y % 10 - 1, 12, PersianDate.DaysInMonth(y - y % 10 - 1, 12)); } if (newDisplayDate >= DisplayDateStart && newDisplayDate <= DisplayDateEnd) { this.SetCurrentValue(DisplayDateProperty, newDisplayDate); } } catch (ArgumentOutOfRangeException) { } }