windowsforms-devexpress-dot-xtraeditors-dot-controls-dot-calendarcontrolbase-a4c88e67.md
Gets or sets the ICalendarCellStyleProvider object that allows you to customize the appearance of certain dates.
Namespace : DevExpress.XtraEditors.Controls
Assembly : DevExpress.XtraEditors.v25.2.dll
NuGet Package : DevExpress.Win.Navigation
[DefaultValue(null)]
[DXCategory("Behavior")]
public ICalendarCellStyleProvider CellStyleProvider { get; set; }
<DefaultValue(Nothing)>
<DXCategory("Behavior")>
Public Property CellStyleProvider As ICalendarCellStyleProvider
| Type | Default | Description |
|---|---|---|
| DevExpress.XtraEditors.Controls.ICalendarCellStyleProvider | null |
The object that allows you to customize the appearance of certain dates.
|
When an ICalendarCellStyleProvider object is assigned to the CellStyleProvider property, the ICalendarCellStyleProvider.UpdateAppearance method is called before painting each cell in the calendar. Using this method, you can provide a custom appearance for certain dates.
See the Editors Main Demo for an example of customizing the appearance of dates in the CalendarControl.
This example shows how to create a custom CellStyleProvider object that customizes the cell appearance in the CalendarControl and DateEdit controls.Calendar cells that correspond to certain holiday dates are highlighted in a custom manner.
using DevExpress.Utils;
using DevExpress.Utils.Design;
using DevExpress.XtraEditors.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calendar_CellStyleProvider {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
dateEdit1.DateTime = calendarControl1.DateTime = new DateTime(2016, 12, 31);
dateEdit1.Properties.CellStyleProvider = calendarControl1.CellStyleProvider = new CustomCellStyleProvider();
ContextButton cb = new ContextButton() {
Alignment = ContextItemAlignment.TopNear, Visibility=ContextItemVisibility.Hidden
};
calendarControl1.CellSize = new Size(50, 50);
calendarControl1.ContextButtons.Add(cb);
calendarControl1.ContextButtonCustomize += CalendarControl1_ContextButtonCustomize;
}
private void CalendarControl1_ContextButtonCustomize(object sender, CalendarContextButtonCustomizeEventArgs e) {
string holidayText;
if (Holidays.IsHoliday(e.Cell.Date, out holidayText)) {
e.Item.Glyph = global::Calendar_CellStyleProvider.Properties.Resources.Party;
e.Item.Visibility = ContextItemVisibility.Visible;
e.Item.ToolTip = holidayText;
e.Item.ShowToolTips = true;
}
}
}
public static class Holidays {
public static bool IsHoliday(DateTime dt, out string holidayText) {
holidayText = "";
//New Year's Day
if (dt.Day == 1 && dt.Month == 1) holidayText = "New Year's Day";
//Independence Day
if (dt.Day == 4 && dt.Month == 7) holidayText = "Independence Day";
//Veterans Day
if (dt.Day == 11 && dt.Month == 11) holidayText = "Veterans Day";
//Christmas
if (dt.Day == 25 && dt.Month == 12) holidayText = "Christmas";
return !string.IsNullOrEmpty(holidayText);
}
}
public class CustomCellStyleProvider : ICalendarCellStyleProvider {
public void UpdateAppearance(CalendarCellStyle cell) {
string holidayText;
if(Holidays.IsHoliday(cell.Date, out holidayText)) {
cell.Appearance.ForeColor = Color.Yellow;
cell.Appearance.Font = new Font(cell.Appearance.Font, FontStyle.Bold);
if (cell.Active)
cell.Appearance.BackColor = Color.HotPink;
else
cell.Appearance.BackColor = Color.LightPink;
}
}
}
}
Imports DevExpress.Utils
Imports DevExpress.Utils.Design
Imports DevExpress.XtraEditors.Controls
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Namespace Calendar_CellStyleProvider
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
calendarControl1.DateTime = New Date(2016, 12, 31)
dateEdit1.DateTime = calendarControl1.DateTime
calendarControl1.CellStyleProvider = New CustomCellStyleProvider()
dateEdit1.Properties.CellStyleProvider = calendarControl1.CellStyleProvider
Dim cb As New ContextButton() With {.Alignment = ContextItemAlignment.TopNear, .Visibility=ContextItemVisibility.Hidden}
calendarControl1.CellSize = New Size(50, 50)
calendarControl1.ContextButtons.Add(cb)
AddHandler calendarControl1.ContextButtonCustomize, AddressOf CalendarControl1_ContextButtonCustomize
End Sub
Private Sub CalendarControl1_ContextButtonCustomize(ByVal sender As Object, ByVal e As CalendarContextButtonCustomizeEventArgs)
Dim holidayText As String = Nothing
If Holidays.IsHoliday(e.Cell.Date, holidayText) Then
e.Item.Glyph = My.Resources.Party
e.Item.Visibility = ContextItemVisibility.Visible
e.Item.ToolTip = holidayText
e.Item.ShowToolTips = True
End If
End Sub
End Class
Public NotInheritable Class Holidays
Private Sub New()
End Sub
Public Shared Function IsHoliday(ByVal dt As Date, ByRef holidayText As String) As Boolean
holidayText = ""
'New Year's Day
If dt.Day = 1 AndAlso dt.Month = 1 Then
holidayText = "New Year's Day"
End If
'Independence Day
If dt.Day = 4 AndAlso dt.Month = 7 Then
holidayText = "Independence Day"
End If
'Veterans Day
If dt.Day = 11 AndAlso dt.Month = 11 Then
holidayText = "Veterans Day"
End If
'Christmas
If dt.Day = 25 AndAlso dt.Month = 12 Then
holidayText = "Christmas"
End If
Return Not String.IsNullOrEmpty(holidayText)
End Function
End Class
Public Class CustomCellStyleProvider
Implements ICalendarCellStyleProvider
Public Sub UpdateAppearance(ByVal cell As CalendarCellStyle) Implements ICalendarCellStyleProvider.UpdateAppearance
Dim holidayText As String = Nothing
If Holidays.IsHoliday(cell.Date, holidayText) Then
cell.Appearance.ForeColor = Color.Yellow
cell.Appearance.Font = New Font(cell.Appearance.Font, FontStyle.Bold)
If cell.Active Then
cell.Appearance.BackColor = Color.HotPink
Else
cell.Appearance.BackColor = Color.LightPink
End If
End If
End Sub
End Class
End Namespace
The following code snippets (auto-collected from DevExpress Examples) contain references to the CellStyleProvider property.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
winforms-scheduler-country-specific-work-week-holidays/CS/Form1.cs#L28
dateNavigator1.CellStyleProvider = new CustomCellStyleProvider(schedulerControl1.WorkDays);
dateNavigator1.HighlightHolidays = true;
winforms-calendar-dateedit-cell-appearance-customization/CS/Calendar_CellStyleProvider/Form1.cs#L22
dateEdit1.DateTime = calendarControl1.DateTime = new DateTime(2016, 12, 31);
dateEdit1.Properties.CellStyleProvider = calendarControl1.CellStyleProvider = new CustomCellStyleProvider();
winforms-calendar-dateedit-cell-appearance-customization/VB/Calendar_CellStyleProvider/Form1.vb#L25
dateEdit1.DateTime = calendarControl1.DateTime
calendarControl1.CellStyleProvider = New CustomCellStyleProvider()
dateEdit1.Properties.CellStyleProvider = calendarControl1.CellStyleProvider
winforms-scheduler-country-specific-work-week-holidays/VB/Form1.vb#L27
schedulerControl1.ActiveViewType = SchedulerViewType.Day
dateNavigator1.CellStyleProvider = New CustomCellStyleProvider(schedulerControl1.WorkDays)
dateNavigator1.HighlightHolidays = True
See Also