Blogs

Blogs

DevExpress XAF – Scheduler Control Change Event Colours Depending on a Custom Field

14th April 2015

By default when using the Win Scheduler List Editor List Editor Type when viewing your Events using the Scheduler Control, the colour of the Event will depend on the colour set up against your Event Type. It may be required for the colour of the Event within the listing to change depending on a custom field such as an Event Status. The code snippet below examples changing the Event colour depending on the value selected within a custom field against your Event, it needs to be implemented within a View Controller against the Module.Win project.

using System.Drawing;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Scheduler.Win;
using DevExpress.Persistent.Base.General;
using DevExpress.XtraScheduler;
using DevExpress.XtraScheduler.Drawing;
 
/// <summary>
/// Scheduler Calendar View Controller
/// </summary>
public partial class SchedulerCalendarViewController : ObjectViewController<ListView, IRecurrentEvent>
{
    /// <summary>
    /// Initializes a new instance of the <see cref="SchedulerCalendarViewController"/> class.
    /// </summary>
    public SchedulerCalendarViewController()
    {
        this.InitializeComponent();
        this.RegisterActions(this.components);
    }
 
    /// <summary>
    /// Called when [view controls created].
    /// </summary>
    protected override void OnViewControlsCreated()
    {
        base.OnViewControlsCreated();
 
        SchedulerListEditor listEditor = View.Editor as SchedulerListEditor;
 
        if (listEditor != null)
        {
            SchedulerControl scheduler = listEditor.SchedulerControl;
 
            if (scheduler != null)
            {
                scheduler.CustomDrawAppointmentBackground += new CustomDrawObjectEventHandler(this.SchedulerControl_CustomDrawObject);
            }
        }
    }
 
    /// <summary>
    /// Handles the CustomDrawObject event of the SchedulerControl control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="CustomDrawObjectEventArgs"/> instance containing the event data.</param>
    private void SchedulerControl_CustomDrawObject(object sender, CustomDrawObjectEventArgs e)
    {
        AppointmentViewInfo aptViewInfo = e.ObjectInfo as AppointmentViewInfo;
 
        if (aptViewInfo != null)
        {
            MyEvent myEvent = this.ObjectSpace.GetObjectByKey<MyEvent>(aptViewInfo.Appointment.Id);
 
            if (myEvent != null)
            {
                if (myEvent.MyCustomField == "ValueOne")
                {
                    Brush fillBrush = e.Cache.GetSolidBrush(System.Drawing.Color.LightBlue);
                    e.Graphics.FillRectangle(fillBrush, e.Bounds);
 
                    if (aptViewInfo.Selected)
                    {
                        Pen selectedPen = e.Cache.GetPen(System.Drawing.Color.Black);
 
                        Brush selectedBrush = e.Cache.GetSolidBrush(System.Drawing.Color.LightSlateGray);
                        e.Graphics.FillRectangle(selectedBrush, e.Bounds);
                    }
                }
                else
                {
                    Brush fillBrush = e.Cache.GetSolidBrush(System.Drawing.Color.LightGreen);
                    e.Graphics.FillRectangle(fillBrush, e.Bounds);
 
                    if (aptViewInfo.Selected)
                    {
                        Pen selectedPen = e.Cache.GetPen(System.Drawing.Color.Black);
 
                        Brush selectedBrush = e.Cache.GetSolidBrush(System.Drawing.Color.LightSlateGray);
                        e.Graphics.FillRectangle(selectedBrush, e.Bounds);
                    }
                }
 
                e.Handled = true;
            }
        }
    }
}
This website uses cookies to provide the best possible experience. By continuing to use our website you are agreeing to our use of cookies.