/// <summary> /// 构造日历界面 /// </summary> private void BuildCalendarUI() { //返回指定月份的总天数 int daysInMonth = _cultureCalendar.GetDaysInMonth(_displayStartDate.Year, _displayStartDate.Month); //返回展示日期的星期数 _startDayOffset = (int)Enum.ToObject(typeof(DayOfWeek), _displayStartDate.DayOfWeek); //总周数 int weekCount = 0; var weekRowCtrl = new WeekControl(); GrdMonthView.Children.Clear(); SetMonthGridRowDefinition(daysInMonth, _startDayOffset); LblMonth.Content = string.Format("{0}年{1}月", _displayYear, _displayMonth); for (int i = 1; i <= daysInMonth; i++) { //算出是星期几 int week = (i + _startDayOffset - 1) % 7; //因为第一次已经有周控件了,所以i != 1 if ((i != 1) && (0 == week)) { //开始新一周,将上周的控件加入 Grid.SetRow(weekRowCtrl, weekCount); GrdMonthView.Children.Add(weekRowCtrl); weekRowCtrl = new WeekControl(); weekCount += 1; } //添加日控件 var dayControl = new DayControl { LblDayNumber = { Content = i.ToString(CultureInfo.InvariantCulture) }, Tag = i }; dayControl.MouseDoubleClick += DayControl_DoubleClick; //如果为当天,设置不同的样式 if (new DateTime(_displayYear, _displayMonth, i) == DateTime.Today) { dayControl.BdrDayLabel.Background = (Brush)dayControl.TryFindResource("TodayBrush"); dayControl.SplDateTimeEvents.Background = Brushes.Wheat; } int iday = i; if (DateTimeEventControlFactory != null) { var eventsInDay = from e in _dateTimeEvents where DayEqual(e.HappenTime, DisplayStartDate.AddDays(iday - 1)) select e; foreach (var e in eventsInDay) { var control = DateTimeEventControlFactory.GetControl(e); if (control != null) { dayControl.SplDateTimeEvents.Children.Add(control.EventControl); } } } Grid.SetColumn(dayControl, (i - (weekCount * 7)) + _startDayOffset); weekRowCtrl.GrdWeek.Children.Add(dayControl); } //加入最后一周 Grid.SetRow(weekRowCtrl, weekCount); GrdMonthView.Children.Add(weekRowCtrl); }