如何更改所有月份所有工作日的颜色属性

if(e.Day.Date.DayOfWeek == DayOfWeek.Monday) { e.cell.BackColor=System.Drwaing.Color.Red; }

我正在尝试这个代码,但它只更改单个月的属性,我想在一年中的所有月份更改所有DayOfweek。

您需要使用Calendar.DayStyle属性为显示的月份中的日期设置样式属性(包括颜色)。

另请注意,如果您没有为当前显示的月份中的日期指定不同的样式,则这些日期也将使用DayStyle属性指定的样式显示。

    

如果您希望其他月份的日期以不同的颜色显示,请使用: Calendar.OtherMonthDayStyle

    

最后,像往常一样,您还可以在代码中设置颜色属性。 在这种情况下使用: Calendar.DayRender事件。

 void Calendar1_DayRender(Object sender, DayRenderEventArgs e) { // Change the background color of the days in other Months // to yellow. if (e.Day.IsOtherMonth) { e.Cell.BackColor=System.Drawing.Color.Yellow; } if (!e.Day.IsOtherMonth) // color to red for current month { e.Cell.BackColor=System.Drawing.Color.Red; } } 

最后, 在日历中浏览月份时 ,请使用事件: Calendar.VisibleMonthChanged ,每次用户更改月份时都会引发该事件。

标记::

  

码::

 protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e) { Calendar1.OtherMonthDayStyle.BackColor = System.Drawing.Color.Yellow; Calendar1.DayStyle.BackColor = System.Drawing.Color.Red; }