Back to Devexpress

RepositoryItemDateEdit.CellStyleProvider Property

windowsforms-devexpress-dot-xtraeditors-dot-repository-dot-repositoryitemdateedit-ce14dc9f.md

latest10.0 KB
Original Source

RepositoryItemDateEdit.CellStyleProvider Property

Gets or sets the ICalendarCellStyleProvider object that allows you to customize the appearance of certain dates.

Namespace : DevExpress.XtraEditors.Repository

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DefaultValue(null)]
[DXCategory("Appearance")]
public ICalendarCellStyleProvider CellStyleProvider { get; set; }
vb
<DefaultValue(Nothing)>
<DXCategory("Appearance")>
Public Property CellStyleProvider As ICalendarCellStyleProvider

Property Value

TypeDefaultDescription
DevExpress.XtraEditors.Controls.ICalendarCellStyleProvidernull

The object that allows you to customize the appearance of certain dates.

|

Remarks

When an ICalendarCellStyleProvider object is assigned to the CellStyleProvider property, the ICalendarCellStyleProvider.UpdateAppearance method is called before painting each cell in the DateEdit control’s dropdown calendar. Using this method, you can provide a custom appearance for certain dates.

Example

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.

View Example

csharp
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;
            }
        }
    }
}
vb
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 snippet (auto-collected from DevExpress Examples) contains a reference 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-calendar-dateedit-cell-appearance-customization/CS/Calendar_CellStyleProvider/Form1.cs#L22

csharp
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#L26

vb
calendarControl1.CellStyleProvider = New CustomCellStyleProvider()
dateEdit1.Properties.CellStyleProvider = calendarControl1.CellStyleProvider

See Also

CellStyleProvider

RepositoryItemDateEdit Class

RepositoryItemDateEdit Members

DevExpress.XtraEditors.Repository Namespace