Example #1
0
        public CalendarMonth(int year, int month)
        {
            this.InitializeComponent();

            this._calendarDays = new Dictionary <string, CalendarDay>();
            this._coloredDays  = new List <CalendarDay>();
            this._headerDays   = new List <CalendarDay>();
            this.Year          = year;
            this.Month         = month;

            int      days = DateTime.DaysInMonth(year, month);
            DateTime firstActualDayOfMonth = new DateTime(year, month, 1);

            // Since the first day of the month could be in the middle of the week, we'll still need to
            // add in days for the preivous month which go before it
            DateTime startingDayOfCalMonth = firstActualDayOfMonth.AddDays(
                numDayOfWeek(new DateTime(year, month, 1).DayOfWeek) * -1         // multiply by -1 to subtract
                );

            DateTime currentDayOfCalMonth = startingDayOfCalMonth;

            for (int i = 0; i < ROWS; i++)
            {
                for (int j = 0; j < COLS; j++)
                {
                    CalendarDay cd = new CalendarDay(currentDayOfCalMonth.Year, currentDayOfCalMonth.Month, currentDayOfCalMonth.Day);
                    cd.SetValue(Grid.RowProperty, i);
                    cd.SetValue(Grid.ColumnProperty, j);


                    // if we're doing the days row
                    if (i == 0)
                    {
                        DayOfWeek nextday = (DayOfWeek)j;
                        cd.DayText         = nextday.ToString();
                        cd.DayValueSnapped = nextday.ToString().Substring(0, 3); // add the three letter rep of the day
                        cd.IsDayHeader();

                        _headerDays.Add(cd);
                    }

                    // if we're no longer on the rows with the days "Sun --> Sat"
                    else
                    {
                        cd.DayValue = currentDayOfCalMonth.Day.ToString();

                        // if the current day is in the past month
                        if (currentDayOfCalMonth.Month != month)
                        {
                            cd.IsGrayed = true;
                        }

                        // if this is today
                        if (CalendarDay.IsToday(cd))
                        {
                            SetToday(cd);
                        }

                        // add to internal Dictionary
                        _calendarDays.Add(GetCalendarDayGUID(currentDayOfCalMonth.Year, currentDayOfCalMonth.Month, currentDayOfCalMonth.Day), cd);

                        // increment days
                        currentDayOfCalMonth = currentDayOfCalMonth.AddDays(1);
                    }

                    // add to UI
                    CalendarMonthGrid.Children.Add(cd);
                }
            }
        }