Skip to main content

SchedulerControl.AppointmentAdding Event

Occurs before the user has added appointments to the scheduler.

Namespace: DevExpress.Xpf.Scheduling

Assembly: DevExpress.Xpf.Scheduling.v24.1.dll

NuGet Package: DevExpress.Wpf.Scheduling

Declaration

public event AppointmentAddingEventHandler AppointmentAdding

Event Data

The AppointmentAdding event's data class is AppointmentAddingEventArgs. The following properties provide information specific to this event:

Property Description
Appointments Provides access to the collection of appointments the user attempts to add to the scheduler.
Cancel Gets or sets whether the event should be canceled. Inherited from CancelRoutedEventArgs.
CanceledAppointments Provides access to the collection of appointments that should be excluded from being added to the scheduler.
ConflictedAppointments Returns the collection of appointments that are conflicting with the current appointments.
Handled Gets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. Inherited from RoutedEventArgs.
OriginalSource Gets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
RoutedEvent Gets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
Source Gets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs.

The event data class exposes the following methods:

Method Description
InvokeEventHandler(Delegate, Object) When overridden in a derived class, provides a way to invoke event handlers in a type-specific way, which can increase efficiency over the base implementation. Inherited from RoutedEventArgs.
OnSetSource(Object) When overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. Inherited from RoutedEventArgs.

Remarks

To prevent specific appointments from being added to the scheduler, add them to the CanceledAppointments collection.

The code snippet below demonstrates how to implement custom validation:

 AppointmentAdding += (d, e) => { 
    foreach(var apt in e.Appointments) { //each appointment which is about to be added
        bool res = Validate(apt); //is validated by a custom method 
        if(!res) //if the validation fails 
            e.CanceledEditAppointments.Add(apt); //this appointment is not added to the scheduler
    } 
}

Example

SchedulerControl provides the AppointmentAdding and AppointmentEditing events. You can use them to implement validation. This example illustrates how you can show a warning message to users when an appointment intersects the lunch time.

The lunch time is defined as a recurrent Time Region Item:

<dxsch:SchedulerControl.TimeRegionItems>
    <dxsch:TimeRegionItem
            Type ="Pattern" 
            Start="1/1/2019 13:00:00" End="1/1/2019 14:00:00" 
            RecurrenceInfo="{dxsch:RecurrenceDaily Start='1/1/2019 13:00:00', ByDay=WorkDays}" 
            BrushName="{x:Static dxsch:DefaultBrushNames.TimeRegion3Hatch}"/>
</dxsch:SchedulerControl.TimeRegionItems>

The validation logic is implemented in the SchedulerValidationService class which is a DialogService class descendant. If an appointment intersects the lunch time, the service displays a dialog window and allows the user to cancel changes either in all appointments or only in the conflicted appointments. Users can also click the Ignore button to override validation and save changes:

bool ProcessAppointments(IReadOnlyList<AppointmentItem> appts, IList<AppointmentItem> itemsToCancel) {
    var toCancel = new List<AppointmentItem>();
    foreach (var item in appts){
        var range = new DateTimeRange(item.Start, item.End);
        var startLunchRange = new DateTimeRange(item.Start.Date.AddHours(13), item.Start.Date.AddHours(14));
        var endLunchRange = new DateTimeRange(item.End.Date.AddHours(13), item.End.Date.AddHours(14));
        if (range.Intersect(startLunchRange).Duration.Ticks != 0 
            || range.Intersect(endLunchRange).Duration.Ticks != 0
            || range.Duration.Hours > 23)
            toCancel.Add(item);
    }

    if (toCancel.Count > 0) {
        var Cancel = new UICommand() { Caption = "Cancel", IsDefault = true };
        var CancelConflicts = new UICommand() { Caption = "Cancel Conflicts" };
        var Ignore = new UICommand() { Caption = "Ignore", IsCancel = true };
        var result = this.ShowDialog(new List<UICommand>() { Cancel, CancelConflicts, Ignore },
            "Warning",
            "WarningUserControl",
            "The following appointment(-s) intersects the lunch time:\n\n"
            + string.Join("\n", toCancel.Select(c => c.Subject)) +
            "\n\nClick 'Cancel' to discard all changes.\nClick 'Cancel Conflicts' to cancel changes only in these appointment(-s).");

        if (result == Cancel)
            return false;

        if (result == CancelConflicts)
            foreach (var item in toCancel)
                itemsToCancel.Add(item);
    }
    return true;
}

When the ProcessAppointments method returns False, the e.Cancel property is set to True in the AppointmentAdding or AppointmentEditing event handlers. If the user chooses to cancel changes only for the conflicted appointments, these appointments are added to the e.CanceledAppointments or e.CanceledEditAppointments collections:

private void Scheduler_AppointmentAdding(object sender, AppointmentAddingEventArgs e) {
    e.Cancel = !ProcessAppointments(e.Appointments, e.CanceledAppointments);
}

private void Scheduler_AppointmentEditing(object sender, AppointmentEditingEventArgs e) {
    e.Cancel = !ProcessAppointments(e.EditAppointments, e.CanceledEditAppointments);
}
See Also