Example #1
0
        public void DisplayCalendar(int year, int month)
        {
            int dayNum = DateTime.DaysInMonth(year, month);

            calendarDisplayUniformGrid = GetCalendarUniformGrid(MainWindow.CalendarListBox);

            DateTime dateTime = new DateTime(year, month, 1);

            calendarDisplayUniformGrid.FirstColumn = (int)(dateTime.DayOfWeek);
            List <DateEntry> mainDateList       = mainCalendar.GetDaysOfMonth(year, month);
            List <DateEntry> subsidiaryDateList = null;

            if (subsidiaryCalendar != null)
            {
                subsidiaryDateList = subsidiaryCalendar.GetDaysOfMonth(year, month);
            }

            for (int i = 0; i < dayNum; i++)
            {
                Label mainDateLabel = new Label();
                mainDateLabel.HorizontalAlignment = HorizontalAlignment.Center;
                mainDateLabel.VerticalAlignment   = VerticalAlignment.Center;
                mainDateLabel.Background          = Brushes.Black;
                mainDateLabel.Padding             = new Thickness(0, 0, 0, 0);
                mainDateLabel.Margin = new Thickness(0, 0, 0, 0);

                //This label is used to hold the holiday string.
                Label hiddenLabel = new Label();
                hiddenLabel.HorizontalAlignment = HorizontalAlignment.Stretch;
                hiddenLabel.VerticalAlignment   = VerticalAlignment.Stretch;
                hiddenLabel.Visibility          = Visibility.Collapsed;

                //If the application is not running in zh-CN env,
                //it can display the date number bigger.
                mainDateLabel.FontSize = (localCultureInfo.ToString() == "zh-CN") ? 20 : 25;

                //Weekend should be dispaly in red color.
                if (IsWeekEndOrFestival(ref dateTime, mainDateList, i))
                {
                    mainDateLabel.Foreground = Brushes.Red;
                    if (mainDateList[i].IsFestival)
                    {
                        hiddenLabel.Content = mainDateList[i].Text;
                    }
                }
                else
                {
                    hiddenLabel.Content      = "";
                    mainDateLabel.Foreground = Brushes.White;
                }

                mainDateLabel.Content = mainDateList[i].DateOfMonth.ToString(NumberFormatInfo.CurrentInfo);

                //If the application is running in a non zh-CN locale, display no lunar calendar.
                Label subsidiaryDateLabel = null;
                if (subsidiaryDateList != null)
                {
                    subsidiaryDateLabel = new Label();
                    subsidiaryDateLabel.HorizontalAlignment = HorizontalAlignment.Center;
                    subsidiaryDateLabel.VerticalAlignment   = VerticalAlignment.Center;
                    subsidiaryDateLabel.Background          = Brushes.Black;
                    subsidiaryDateLabel.Padding             = new Thickness(0, 0, 0, 0);
                    subsidiaryDateLabel.FontSize            = 13;

                    //Control the festival date to be red.
                    subsidiaryDateLabel.Foreground = subsidiaryDateList[i].IsFestival ? Brushes.Red : Brushes.White;
                    subsidiaryDateLabel.Content    = subsidiaryDateList[i].Text;
                }

                //Compose the final displaying unit.
                StackPanel stackPanel = new StackPanel();
                stackPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
                stackPanel.VerticalAlignment   = VerticalAlignment.Stretch;

                stackPanel.Children.Add(hiddenLabel);
                stackPanel.Children.Add(mainDateLabel);
                if (subsidiaryDateLabel != null)
                {
                    stackPanel.Children.Add(subsidiaryDateLabel);
                }
                MainWindow.CalendarListBox.Items.Add(stackPanel);

                //Display the current day in another color
                if (dateTime.Date == DateTime.Now.Date)
                {
                    mainDateLabel.Foreground = Brushes.DarkOrange;
                    if (subsidiaryDateLabel != null)
                    {
                        subsidiaryDateLabel.Foreground = Brushes.DarkOrange;
                    }
                }
                dateTime = dateTime.AddDays(1);
            }
        }